Scale and Precision from Number
javascript, jquery
Solution
var x = 1234.567;
var parts = x.toString().split('.');
parts[0].length; // output: 4 for 1234
parts[1].length; // output: 3 for 567
NOTE
Javascript has toPrecision() method that gives to a number with specified length.
For example:
var x = 1234.567;
x.toPrecision(4); // output: 1234
x.toPrecision(5); // output: 1234.5
x.toPrecision(7); // output: 1234.56
But
x.toPrecision(5); // output: 1235
x.toPrecision(3); // output: 1.23e+3
and so on.
According to comment
Is there a way to check that the string contain `.`?
var x = 1234.567
x.toString().indexOf('.'); // output: 4
Note
`.indexof()` return first index of target else `-1`.
Problem
I'm looking to get the scale and precision from a number in the following example. `var x = 1234.567;` I'm not seeing any `.scale` or `.precision` functions built in and I'm not sure what the best way to right one is.