javascript to eliminate thousand separator (comma)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carmen Z.

    javascript to eliminate thousand separator (comma)

    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="Javas cript" 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??
  • Hogne Titlestad

    #2
    Re: javascript to eliminate thousand separator (comma)

    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.

    Comment

    • Michael Winter

      #3
      Re: javascript to eliminate thousand separator (comma)

      On 11 Jan 2005 01:37:22 -0800, Carmen Z. <ladyphotoartis t@yahoo.com> wrote:
      [color=blue]
      > 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?[/color]

      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]
      [color=blue]
      > <script language="Javas cript" type="text/javascript">[/color]

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

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Michael Winter

        #4
        Re: javascript to eliminate thousand separator (comma)

        On Tue, 11 Jan 2005 12:13:33 +0100, Hogne Titlestad <hogne@blest.no > wrote:

        Please quote relevant text when replying to a post.
        [color=blue]
        > Here is your solution![/color]

        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.
        [color=blue]
        > 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.[/color]

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

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

        which will remove all commas in the string.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...