This one is really confusing me.
var a = 0.0003;
var b = a.toPrecision(3);
I was expecting b to be "0.000300", and that's exactly what I got in Firefox, IE and Chrome. But not in Edge. Edge gave me "0.000200".
Anyone have any idea what's going on?
jsfiddle here if anyone wants to take a look: http://jsfiddle.net/dajp/2b47qLg0/
(I'm using Edge 40.15063.0.0 with EdgeHTML 15.15063 if that makes a difference.)
Many thanks.
This is probably not the best method but it fixes the problem on edge.
This is just a modification to your jsfiddle's javascript:
runWeirdness = function() {
a = 0.0003;
b = a.toFixed(3 + decimalPlaces(a));
document.getElementById("para").innerHTML = "Answer is: " + b;
}
function decimalPlaces(num) {
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}
The basic difference between toFixed() and toPrecision() is that toFixed(n) provides an nth length after the decimal point while toPrecision(x) provides x total length, including before the decimal point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With