Click to See Complete Forum and Search --> : Zeros after a Decimal


robn
01-09-2003, 10:10 AM
Does anyone know how to display Zeros after a decimal? IE and Netscape both drop trailing zeros which makes currency values look - not so great (ex: 25.5 instead of 25.50).

I managed to get IE 5.5 to display the zeros by using "toFixed()" - but that won't work in Netscape (and I'm not sure about previous versions of IE).

Any advice will be greatly appreciated.

Thanks.

AdamGundry
01-09-2003, 11:25 AM
You could convert the decimal to a string and then test for the position of the decimal point relative to the length of the string, adding zeroes if necessary, something like this:


var decimal = 25.5;
var str = decimal.toString();

if (str.indexOf(".") > str.length - 2){
str = str + "0";
}


This will only add one zero - you might need it to loop and add more than one.

Adam

robn
01-09-2003, 12:42 PM
Adam - Thanks for the info.

I tried out your suggestion and modifyied it to suit what I was working on - It works like a charm.

Thanks again!!