• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Javascript math question...

Ichinisan

Lifer
I'm sure this is really basic, but I've been messing around with parseInt, toInt, parseFloat, toString, etc and can't figure out what's happening here.

alert(3*19.95);

"59.849999999999994"

It should be "59.85" ...so what's with the bad math?
 
I hate dealing with floating points in jscript. If I recall correctly I used this function to get the value I actually wanted:

Code:
alert(Round(3*19.95, 2));


function Round(value, decimals) {
    return (Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals));
}
 
I hate dealing with floating points in jscript. If I recall correctly I used this function to get the value I actually wanted:

Code:
alert(Round(3*19.95, 2));


function Round(value, decimals) {
    return (Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals));
}

It's strange that this is rounding at all when I'm not dividing and it's a fairly small number without a lot of precision. Anyway, I had to do something similar to:

((3*1995)/100)

I would expect the extra division operation to increase the chances of a math problem...but this works for some reason.
 
A computer does nto represent 0.95 as 0.95 in memory. It uses something like 0.9444449.

If you want your output to be human readable, use String.format().
 
Back
Top