Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rounding to 2 decimal places

0 views
Skip to first unread message

Jennifer

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
Is there a simple way to round to 2 decimal
places? So if I want to calculate the cost of
something and return it.

Jennifer

Jerry Park

unread,
Mar 1, 2000, 3:00:00 AM3/1/00
to
Jennifer wrote:

Go to my javascript page for a library of formatting functions and
example of usage.

http://www.aptools.com/javascript/

JG

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
You could write a function, but you can only round to an integer value
with Math.round

Lori Crews

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
The easiest way to do this and keep your decimal places is to multiply the
number by 100, use math.round to round it and then divide the number by 100.
The only problem is that it won't keep a trailing zero - in other words
$10.50 would come back as $10.5. Here's an example of code that I used to
round a dollar amount and then add the trailing zero if needed.

// Round the decimal place to a maximum of two digits.

var FixedDecimalsA1 = (FixDecimalsA1 * 100);
var FixedDecimalsA2 = Math.round(FixedDecimalsA1);
var FixedDecimalsA3 = (FixedDecimalsA2 / 100);
var FixedDecimalsB1 = (FixDecimalsB1 * 100);
var FixedDecimalsB2 = Math.round(FixedDecimalsB1);
var FixedDecimalsB3 = (FixedDecimalsB2 / 100);

// If the number of decimal places in the dollar amounts is only 1 then
add a zero to it to
// make it a conventional dollar amount.

var strFixedDecimalsA3 = FixedDecimalsA3.toString();
var FixedDecimalsA3Test = strFixedDecimalsA3.indexOf('.');

if (FixedDecimalsA3Test != "-1") {

if (strFixedDecimalsA3.length != FixedDecimalsA3Test + 3) {
FixedDecimalsA3 = FixedDecimalsA3 + "0";
}

}

var strFixedDecimalsB3 = FixedDecimalsB3.toString();
var FixedDecimalsB3Test = strFixedDecimalsB3.indexOf('.');

if (FixedDecimalsB3Test != "-1") {

if (strFixedDecimalsB3.length != FixedDecimalsB3Test + 3) {
FixedDecimalsB3 = FixedDecimalsB3 + "0";
}

}

// Plug in the amounts in the appropriate fields.

document.GetInfo.Life_Amount2.value = FixedDecimalsA3;
document.GetInfo.Life_Cost.value = FixedDecimalsB3;

}

Hope that helps.

Lori

JG wrote in message <38BDE920...@home.com>...

Jeff Thies

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
> The easiest way to do this and keep your decimal places is to multiply the
> number by 100, use math.round to round it and then divide the number by 100.
> The only problem is that it won't keep a trailing zero - in other words
> $10.50 would come back as $10.5.

Seems a little arduous!!!

Here's what I wrote and use:

function toCurrency(value){
value=Math.round((value+0)*100);
value=value+"";
var l=value.length;
var m=l-2;
var first=value.substring(0,m);
var last=value.substring(m);
var f="";
if(l<2){f="0";};
return first+"."+f+last;
}

It'll add a zero in front of .49 to make it 0.49 .

But, if I were to do it over, I would use whatever Jerry Park has!

Jeff

0 new messages