mamdouh asked this 2 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


mohlal 2 years ago
2 likes

use toFixed() function

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