0

How to round floating point error in javascript?

2
  • 1
    use toFixed() Method, w3schools.com/jsref/jsref_tofixed.asp Commented Mar 2, 2011 at 13:34
  • …so the error is rounder? Sorry, couldn't resist. ;-) Commented Mar 2, 2011 at 13:46

3 Answers 3

3

use toFixed to round it then convert it back to a number

var num = 1.00000000000000002341;
alert(+num.toFixed(10));

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

To round towards 0 to get a 32 bit signed integer, use x | 0.

There's also the Math.round function which rounds towards the closest integer.

Comments

0

You could use Math.round(number * 1E6) / 1E6 to round up to 6 decimal places.

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.