Javascript ToFixed Function Is Buggy
Posted by admin on
February 14, 2013
I used the toFixed() function in Javascript to round off decimal values to the nth place. However, when I compared the values converted in the Javascript and the values converted in PHP using its own round() function, they were not the same.
Seems like the round() function in Javascript does not give me the right value nor does the toFixed() as well. I did came across a post in the StackOverflow forum where member Jasper de Vries provided his own workaround combining both round() and toFixed().
function roundedToFixed(_float, _digits){ var rounder = Math.pow(10, _digits); return (Math.round(_float * rounder) / rounder).toFixed(_digits); }
That should fix the issue.
Donations appreciated. Every little $ helps. Or click Google +1.









