mamdouh asked this 4 years ago

Set decimal precision of result for Javascript floating point addition

I am trying to add two floating point numbers

var sum = parseFloat(a) + parseFloat(b);

How do i set the decimal precision of the result to one?

Eg: 0.1+0.2 should give 0.3

Best Answer by mohlal 4 years ago

use toFixed() function

var result = parseFloat(a+b).toFixed(1);