-5

I am trying to round a number to 2 decemals and it is giving me integers!

var 4 = 10.99 + 89.78899999
total = number(a) + number(b);

Here is what I currently have:

Math.round(total, 2);  // this gives me 101 I need it to show 100.78

What is the trick to do this?

gdoron
  • 147,333
  • 58
  • 291
  • 367
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 3
    var 4 is an invalid name – Max Hudson Feb 05 '13 at 17:46
  • 1
    Why did you think that [`Math.round`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/round) would give you decimal places? – Dancrumb Feb 05 '13 at 17:47
  • As stated below you should probably use `Number.toFixed()` as Math.round only takes one parameter (the value to round) and rounds it to the nearest integer. Using `Math.round()` you'd have to do this to get two decimal places `Math.round(myNumber * 100) / 100)`. – pseudosavant Feb 05 '13 at 17:48

1 Answers1

10

You should use toFixed(2) like with:

alert(12.432432.toFixed(2)); // 12.43

Math.round is for: "Returns the value of a number rounded to the nearest integer."

BTW, 4 isn't a valid variable name in javascript, it can't begin with a number.

What characters are valid for JavaScript variable names?

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367