Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Putting commas in output

    I have an output but need to put a comma for the every three numbers.
    For example:
    220469.06 Should be 220,469.06

    And this:
    2469.06 should be 2,469.06

    and this: 234.03 should stay 234.03

    And this: 2324566.00 should be 2,324,566.00

    My attempt isnt working:


    number = form1.dollarTotal.value;
    newDollarTotal = number.subtring(0,3)+","+number.subtring(4,6);

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've seen regular expressions that do this but I don't understand them so I wrote this

    Code:
    //Rounding example = Make_Number_String_with_Commas(87954778464.7771,'round_result')
    
    function Make_Number_String_with_Commas(input_number, round_results) {
    
    if (round_results) {	
    input_number = Math.round(input_number)
    }
    
    var num_str = '*' + input_number + '*'
    num_str = num_str.substring(1, num_str.length - 1)
    
    var isDecimal = false
    if (num_str.indexOf('.') != -1) {
    isDecimal = true
    var decimal_array = num_str.split('.')
    num_str = decimal_array[0]
    }
    
    var num_str_length = num_str.length
    var array_1 = new Array()
    
    for (var i=0;i<num_str_length;i++) {
    array_1[array_1.length] = num_str.charAt(i)
    }
    
    var array_1_reversed = array_1.reverse()
    array_1_reversed_length = array_1_reversed.length
    
    var array_2 = new Array()
    
    for (var i=0;i<array_1_reversed_length;i++) {
    if (i == 0) {
    array_2[array_2.length] = array_1_reversed[i]
    } else {
    var divide_by_3_str = '*' + i/3 + '*'
    divide_by_3_str = divide_by_3_str.substring(1, divide_by_3_str.length - 1)
    if (divide_by_3_str.indexOf('.') == -1) {
    array_2[array_2.length] = array_1_reversed[i] + ','
    } else {
    array_2[array_2.length] = array_1_reversed[i]
    }
    }
    }
    
    var num_comma_str = ''
    
    array_2 = array_2.reverse()
    
    for (var i=0;i<array_2.length;i++) {
    num_comma_str += array_2[i]
    }
    
    if (isDecimal) {
    num_comma_str = num_comma_str + '.' + decimal_array[1]
    }
    
    return num_comma_str
    return null
    }
    I don't think you can do anything about scientific notation numbers but I hope I'm wrong?

  • #3
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Here is a shorter version:

    Code:
    //-- Shows number in $$xxx,xxx.xx format and pads left side with blanks.
    function currency(anynum,width) {
       anynum=eval(anynum)
       workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
       if (workStr.indexOf(".")==-1){workStr+=".00"}
       dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
       pStr=workStr.substr(workStr.indexOf("."))
       while (pStr.length<3){pStr+="0"}
    
       //--- Adds comma in thousands place.
       if (dNum>=1000) {
          dLen=dStr.length
          dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
       }
    
       //-- Adds comma in millions place.
       if (dNum>=1000000) {
          dLen=dStr.length
          dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
       }
       retval=dStr+pStr
       if (anynum < 0) {
          retval=retval.substring(1,retval.length)
          retval="("+retval+")"        
       }
       retval = "$"+retval
       //--Pad with leading blanks to better align numbers.
       while (retval.length<width){retval=" "+retval}
    
       return retval
    }
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

  • #5
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here's an shorter version.

    Written in actionScript, but it works its all ECMA based, so whatever...

    Code:
    String.prototype.currencyFormat = function() {
    	isNaN(parseFloat(this)) ? c = 0 : c = parseFloat(this);
    	v = (Math.round(Number(c) * 100) / 100).toString();
    	g = v.split(".");
    	t = g[0].split("");
    	for (i = t.length - 1; i > -1; i -= 3) {
    		i != t.length - 1 ? t[i] += "," : null;
    	}
    	f = String(Math.round(Math.abs(v) % 1 * 100));
    	f.length == 1 ? f = ".0" + f : f = "." + f;
    	return t.join("") + f;
    };
    //
    q = "-1999.999gfhd";
    //
    trace(q.currencyFormat());
    //output: -2,000.00
    This is about as simple as I can get it. Currency formatting, and comma insertion.

    enjoy

    -tutash

  • #6
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here is a simple one I cooked up, first one is regular and the second one is for currency
    Code:
    var myamount = 12345678.2134
    myamount= myamount.toString()
    var mylen = myamount.length
    var j = myamount.indexOf('.')>0 ? mylen-myamount.indexOf('.') : 0
    for (var i=3+j; i<18+j; i +=3){
    if (mylen>i){myamount= myamount.slice(0,mylen-i) + "," + myamount.slice(mylen-i)}
    }
    myamount is now 12,345,678.2134

    line 1: We set the value as numeral as that is probably how we get it in a real application.
    line 2: Convert to string for manipulation.
    line 3: How many letters are the in this string.
    line 4: Is there a decimal point?
    line 5: loop 5 times, should be enough as that is quadrillion
    line 6: merge in "," at right places.

    Currency version that force two decimals
    Code:
    function tocurrency(myamount){
     myamount= Math.round(myamount * 100).toString();
     var mylen = myamount.length;
     for (var i=5; i<20; i +=3){
     if (mylen>i){myamount= myamount.slice(0,mylen-i) + "," + myamount.slice(mylen-i)}
     }
     mylen = myamount.length;
     if (mylen<2) {myamount = "0" + myamount;mylen++}
     return "$" + myamount.slice(0,mylen-2) + "." + myamount.slice(mylen-2);
    }
    If you called the function with this example
    document.getElementById('mytotalfield').value= tocurrency(6000 + 578)
    it would show $6,578.00

    I have tested above function with a string and it works OK without parseFloat it first.
    I take it that Math.round() does it's own parseFloat as "1234" * 100 should not work.
    But take in account that strings "1234" + "10" = "123410" and not 1244 in any pre calculations.

    Here is a split each digit to array version.

    Code:
    function tocurrency(d){
     var myamount=[];
     myamount= Math.round(d * 100).toString().split("");
     var mylen = myamount.length;
     if (mylen<2) {myamount.splice(0,0,".","0")}
     else {myamount.splice(mylen-2,0,".")} 
    // if (mylen<3) {myamount.splice(0,0,"0")} // If you want .50 to show 0.50
     for (var i=5; i<20; i +=3){
     if (mylen>i){myamount.splice(mylen-i,0,",")}
     }
     return "$" + myamount.join("");
    }
    Last edited by tonyp12; 02-03-2007 at 10:25 PM. Reason: Fixed a bug when starting value is 0.09 or less

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    This works well using regular expressions:-

    <script type = "text/javascript">

    var num = 1234567890123;

    document.write('<span id="count"></span>');

    document.getElementById('count').innerHTML = addCommas(String(num));

    function addCommas(nStr) {
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1' + ',' + '$2');
    }
    return nStr;
    }

    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •