Search code examples
javascriptnumber-formatting

Javascript round number to 2 decimal places


I have a little problem that I can't seem to get my head round...

I have figure that I need to find what the VAT element of it would be, I have the following jQuery which works but it doesn't fix it to 2 decimal places.

var ThreeMonthPriceFinal = "99.29";
var ThreeMonthPriceVAT = ThreeMonthPriceFinal * 0.2.toFixed(2);
alert(ThreeMonthPriceVAT);

It works out the VAT correctly but adds lots of recurring digits that i don't need... I can't round it up as with VAT you not really supposed to.

http://jsfiddle.net/Xg4Qs/

The Jfiddle shows £19.858000000000004 I need this to show 19.86, i've tried the following but it rounds it up to the whole amount not just 1p.

var ThreeMonthPriceVAT = Math.round(ThreeMonthPriceFinal * 0.2).toFixed(2);

Can anyone help me?


Solution

  • Magic of parentheses

    var ThreeMonthPriceVAT = (ThreeMonthPriceFinal * 0.2).toFixed(2);