SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Displaying 2 decimal points

    Hi,

    I am creating a webpage which calculates some dollar totals. However, if the calculation results in the last decimal point being 0 it does not display it. For example, if I add two numbers and the result is 12.70 it is displayed as 12.7 rather than 12.70. Is there any way to overcome this.

    thankyou,
    Darren.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    im not certain this works but it might:

    Math.round(variable,2);

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This may not be the most elegant solution to the problem but it works.

    var sum= your total
    sum=Math.round(sum*100)/100;
    sum=sum.toString();
    var dot=sum.substring(sum.indexOf('.',0)+1,sum.length);
    if (sum.indexOf('.',0)==-1){sum=sum+'.00'}
    else if (dot.length==1){sum=sum+'0'}

    /*
    // This part will insert commas
    var re = /(-?\d+)(\d{3})/;
    var num = sum.toString();
    while (re.test(num)) {num = num.replace(re,"$1,$2")}
    */


Bookmarks

Posting Permissions

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