By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

javascript to eliminate thousand separator (comma)

P: n/a
do you know how to write a script so that the number that is coming
from database with thousand separator (comma) will be eliminated and
shown to a web page?

so if the value is 1,200, i wanto display it as 1200

suppose %%Raw_Number%% is read off some database, and contains a
number with thousand separator and we want to display the same value
on a web page without thousand separator by using a variable
New_Number

here is something i came up to give the idea. on the third line,
there is the word "contains". i dont think its part of javascript
condition.

**********

<script language="Javascript" type="text/javascript">
var New_Number;
if (%%Raw_Number%% contains ",") {
New_Number = Raw_Number - ",";
}
document.write('New_Number');

************

how can i change the script to have it work??
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here is your solution!

Just modify the one below. If there are more than one comma in the
numbers you will have to pass the replace function more than one time
on the number, as Javascript finds only one instance and exits each
time the function is run.

<script type="text/javascript">
function changeNumber ( num )
{
var newNumb = num.replace ( ",", "." );
return newNumb;
}
</script>

Hogne T.
Jul 23 '05 #2

P: n/a
On 11 Jan 2005 01:37:22 -0800, Carmen Z. <la*************@yahoo.com> wrote:
do you know how to write a script so that the number that is coming from
database with thousand separator (comma) will be eliminated and shown to
a web page?
If the data is taken from a database, then it will be included by a
server-side script. It should be in *that* script that the change is made,
not on the client.

[snip]
<script language="Javascript" type="text/javascript">


The language attribute is deprecated and redundant. Don't use it.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

P: n/a
On Tue, 11 Jan 2005 12:13:33 +0100, Hogne Titlestad <ho***@blest.no> wrote:

Please quote relevant text when replying to a post.
Here is your solution!
Your "solution" replaces commas with decimal separators (periods).
Although the change is trivial, a the OP would, quite reasonably, expect
any proposal to work "as-is", particularly if you declare it to be a
solution.
If there are more than one comma in the numbers you will have to pass
the replace function more than one time on the number, as Javascript
finds only one instance and exits each time the function is run.


That isn't much good if the number of commas is unknown, though. An
alternative is

function changeNumber(num) {
return num.replace(/,/g, '');
}

which will remove all commas in the string.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.