javascript to eliminate thousand separator (comma)

C

Carmen Z.

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??
 
H

Hogne Titlestad

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.
 
M

Michael Winter

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
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top