How can I get the precision of a decimal in js?
This is probably a super easy thing to solve, but I just can't think of an elegant solution right now.
Given the following numbers, how do I find the 开发者_JS百科precision (.0001) of each number.
126.01
1.3450
Update: The number of digits after the decimal.
The output from the top two numbers would be.
.01
.0001
You can't. All Numbers have the standard JavaScript precision.
If you had "126.01" and "1.3450" (i.e. Strings), then you could do something like:
console.log("The precision of " + str_number + " is " + str_number.split('.')[1].length + " decimal places");
Or use 0 if you want significant figures instead.
Maybe try building a simple function like:
function acc(num) {
var splt = num.toString().replace(/\.|$/, '.').split('.');
return splt[1].length
? 1 / Math.pow(10, splt[1].length)
: Math.pow(10, /0*$/.exec(splt[0])[0].length);
}
加载中,请稍侯......
精彩评论