Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to round floating point error in javascript?
use toFixed to round it then convert it back to a number
var num = 1.00000000000000002341; alert(+num.toFixed(10));
DEMO
Add a comment
To round towards 0 to get a 32 bit signed integer, use x | 0.
x | 0
There's also the Math.round function which rounds towards the closest integer.
Math.round
You could use Math.round(number * 1E6) / 1E6 to round up to 6 decimal places.
Math.round(number * 1E6) / 1E6
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.