BootDB

Code | Open Source | Web

Fix Floating Point Precision in JavaScript

toPrecision is a JavaScript built-in function which can be used to set precision of a floating number, example usage:

var number = 123.456789;
number.toPrecision(5); // returns string: '123.46'

To convert string to number use:

Number(number.toPrecision(5));