I have a problem with my client side script not calculating the same values as my server side code:
For example:
var x = (2.85 * .1); alert(x); This gives a figure of 0.28500000000000003
However my server side code (C#) calculates a figures of 0.285 which when rounded to 2 decimal places gives 0.28
If I try and round 0.28500000000000003 to 2 decimal places I get 0.29.
How do I get my Javascript to create a figure that matches my server side code.
It looks like I have to go through 2 lots of rounding - firstly to remove the trailing 3, then the rounding to the required decimal places.
For example:
var x = 0.2850000000003; x = parseFloat(x.toFixed(3)) x = x.toFixed(2) alert(x); Is this the best workaround?
(this is a re-wording of a question I opened and deleted earlier)
0.285should not get rounded to0.28. You must be doing some other operation in C#string str = (2.85 * .1).ToString("R")(R means RoundTrip, it will show all the decimals the double has). You'll see the ,...0003decimalor floating point?