SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: Displaying 2 decimal points
-
Jul 8, 2002, 03:34 #1
- Join Date
- Jul 2002
- Posts
- 26
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jul 8, 2002, 08:01 #2
- Join Date
- Jun 2002
- Location
- Ohio
- Posts
- 5,252
- Mentioned
- 161 Post(s)
- Tagged
- 1 Thread(s)
im not certain this works but it might:
Math.round(variable,2);
-
Jul 8, 2002, 11:45 #3
- 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