Math.round Rounding Error

javascript round to 1 decimal
math.round java
javascript round to nearest 10
math.round c#
round-off error example
tofixed rounding issue
javascript round float
javascript round percentage

I Want to round 1.006 to two decimals expecting 1.01 as output

When i did

var num = 1.006;
alert(Math.round(num,2)); //Outputs 1 
alert(num.toFixed(2)); //Output 1.01


var num =1.106;
alert(Math.round(num,2)); //Outputs 1
alert(num.toFixed(2));; //Outputs 1.11


  • Is it safe to use toFixed() every time ?
  • Is toFixed() cross browser complaint?

Please suggest me.

P.S: I tried searching stack overflow for similar answers, but could not get proper answer.


Why does 1.015 return 1.01 where as 1.045 returns 1.05

var num =1.015;
alert(num.toFixed(2)); //Outputs 1.01
alert(Math.round(num*100)/100); //Outputs 1.01

Where as

var num = 1.045;
alert(num.toFixed(2)); //Outputs 1.04
alert(Math.round(num*100)/100); //Outputs 1.05

Try something like...


1) Multiple the original number by 10^x (10 to the power of x)
2) Apply Math.round() to the result
3) Divide result by 10^x


(to round any number to x decimal points)

Round-off error, This rounding error is a characteristic feature of floating-point computation. Therefore, while We use Math.round() function to round the answer to. closest long  I have encountered a weird case in Math.Round function in VB.Net. I need 32.63 but the function is working in different logic in these cases. I can get the decimal part and make what I want doing something on it. But isn't this too weird, one is rounding to higher, one is rounding to lower. So how can I get 32.63 from 32.625 without messing

I realize this problem is rather old, but I keep running into it even 5 years after the question has been asked.

A working solution to this rounding problem I know of is to convert the number to a string, get the required precision number and round up or down using math rules.

An example where Math.round provides unexpected rounding and an example of string rounding can be found in the following fiddle:

function round(number, decimals = 0) {
  let s = '' + number;
  let dot = s.indexOf('.');
  let start = dot + decimals + 1;
  let dec = Number.parseInt(s.substring(start, start + 1));
  let remainder = dec >= 5 ? 1 / Math.pow(10, decimals) : 0;
  let result = Number.parseFloat(s.substring(0, start)) + remainder;
  return result.toFixed(decimals);

let num = 0.145;
let precision = 2;

console.log('math round', Math.round(num*Math.pow(10, precision))/Math.pow(10, precision));
// 0.145 rounded down to 0.14 - unexpected result
console.log('string round', round(num, precision));
// 0.145 rounded up to 0.15 - expected result

Math.round doesn't work properly here because 0.145 multiplied by 100 is 14.499999999999998, not 14.5. Thus, Math.round will round it down as if it was 14.4. If you convert it to a string and subtract required digit (5), then round it using standard math rules, you will get an expected result of 0.15 (actually, 0.14 + 0.01 = 0.15000000000000002, use "toFixed" to get a nice, round result).

Math.Round Method (System), It won't round the values using Math.round() once it's rounded up by two digits. function fixRoundingError(input, action) { if(Array.isArray(input))  A roundoff error, also called rounding error, is the difference between the result produced by a given algorithm using exact arithmetic and the result produced by the same algorithm using finite-precision, rounded arithmetic. Rounding errors are due to inexactness in the representation of real numbers and the arithmetic operations done with them. This is a form of quantization error. When using approximation equations or algorithms, especially when using finitely many digits to represent real nu

This formula Math.round(num*100)/100 is not always good. Example

Math.round(0.145*100)/100 = 0.14

this is wrong, we want it to be 0.15


The problem is that we have floats like that

0.145 * 100 = 14.499999999999998

step one

so If we round, we need to add a little bit to our product.

0.145 * 100 + 1e-14 = 14.500000000000009

I assume that sometimes the product might be something like 1.000000000000001, but it would not be a problem if we add to it, right?

step two

Calculate how much should we add?

We know float in java script is 17 digits.

let num = 0.145
let a = Math.round(num*100)/100
let b = a.toString().length
let c = 17-b-2
let result = Math.round(num*100 + 0.1**c)/100
console.log('not - ' + a )

(-2) - is just to be sure we are not falling into the same trap of rounding.


let num = 0.145
let result = Math.round(num*100 + 0.1**(17-2-(Math.round(num*100)/100).toString().length))/100

Math.round(), toString(36) , then there would be an error, because JavaScript syntax implies One of the most used operations when working with numbers is rounding. This rounds up or down to the nearest value, similar to Math.round :. Rounding Errors. The most common solutions for rounding to a decimal place is to either use Number.prototype.toFixed(), or multiply the float by some power of 10 in order to leverage Math.round(). Both of these work, except sometimes a decimal of 5 is rounded down instead of up. Number ((1.005).toFixed(2)); // 1 instead of 1.01 Math.round(1.005 * 100)/ 100; // 1 instead of 1.01 A Better Solution

Rounding off errors in Java, JavaScript's Math object provides a method for rounding to whole numbers. If we want to round to a set number of decimal places, then we have to handle This results in rounding errors for the most common approaches to  Midpoint values and rounding conventions. Rounding involves converting a numeric value with a specified precision to the nearest value with less precision. For example, you can use the Round(Double) method to round a value of 3.4 to 3.0, and the Round(Double, Int32) method to round a value of 3.579 to 3.58.

Floating point rounding error - JavaScript, There's some error to be expected here, but by keeping three decimal places, this This new value is rounded up to the nearest integer using math.ceil() , and  This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction. You can specify how Math.Round should round mid-points using an overload which takes a MidpointRounding value.

Numbers, Math.Round , when specifying a digit count, has a bug and does not Math.​Round(0.015, 2, MidpointRounding.AwayFromZero) -> 0.02 This will print the result 0.020000000000000004 while it should just print 0.02 (if you use your calculator). As far as I understood this is due to errors in the floating point multiplication precision. Does anyone have a good solution so that in such case I get the correct result 0.02? I know there are functions like toFixed or rounding would

  • Math.round rounds to the nearest integer.
  • @Jrod: If you refer this fiddle it outputs decimal values also
  • @nlsbshtr: Thanks for pointing it, can you check my edit and help me with second question
  • @Sudarshan your edit has to do with the fact that some decimal numbers cannot be represented to perfect precision. Specifically in your case, 1.015*100 gives you 101.49999999999999 instead of 101.5, causing it to round down instead of up.
  • @jbabey:Thanks for you comment, it solved my problem.
  • Thanks for your answer, BTW how did you arive at 100?
  • @Sudarshan 10^x gives you x decimal points, 10^2 = 100 = 2 decimal points.
  • @jbabey: Thanks for your answer, can you check my edit and can you explain me what is correct approach for rounding
  • @Sudarshan instead of round you can use ceil or floor, ceil to get rounded up and floor to round down, thats more specific than round
  • @nlsbshtr: it seems problem with floating point errors.
  • I confirm, your way is working. Thank you for sharing. However, I bet my solution is better :)