0

I'm trying to round a float up to 2 decimals.

I've 3 pars

a = 0.83;
b = 44.5;
c = a * b

(result on calculator is "36.935". result on source code is "36.934999999999995");

I will need to be rounded up so I end up with the following:

36.94

When I use the following method:

Math.round(c * 100) / 100;

The result is:

36.93

Anybody can help me to round it?

Nguyen
  • 43
  • 6
  • Possible duplicate of [Round to at most 2 decimal places in JavaScript](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript) – Shashank Mar 24 '16 at 05:09

2 Answers2

1

If you are doing Math.round 36.934999999999995, it's always going to be 36.93.

I think this is what you are looking for:

var a = 0.83;
var b = 44.5;
var c = a*b;
console.log(Math.round(c.toFixed(3) * 100) / 100);
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0

I found the answer for my question

function precise_round(num, decimals) {
    var t=Math.pow(10, decimals);   
    return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
}

Thank you Miguel for his answer in https://stackoverflow.com/a/14839776/5118223

Community
  • 1
  • 1
Nguyen
  • 43
  • 6