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().

That should fix the issue.

This simple method will round off a value in N decimal places.

If you happen to come across a decimal value in Java e.g. 0.0008689 but is displayed in a weird way like 8.689E-4, you can use this method to format it in a more readable display. Pretty useful when formatting currencies.

Related Posts Plugin for WordPress, Blogger...