Tried :
var a=10.3;
var b=2.3;
alert(a+b);
but I get 12.600000000000001
. I know JavaScript is loosely typed, but I hope I can do a sum :)
Tried :
var a=10.3;
var b=2.3;
alert(a+b);
but I get 12.600000000000001
. I know JavaScript is loosely typed, but I hope I can do a sum :)
you can use toFixed() method also
var a=10.3;
var b=2.3;
alert((a+b).toFixed(1));
Works in chrome
Multiply to the precision you want then round and divide by whatever you multiplied by:
var a=10.3;
var b=2.3;
alert(Math.round((a+b) * 10) / 10);
It's not about the typing but about the precision of floating point types. You need to round for presentation.
Floating point types are not a good choice if you need exact values. If you want to express currency values express them as cents or use an appropriate library for this.