hiya, is there any way to quickly get number of de...
# getting-started
c
hiya, is there any way to quickly get number of decimal digits from
Double
? I would like the exact number of deciaml digits, ie. 424.234 would return 3 or “234” (either would be good) thanks in advance.
r
Most obvious way would be to get the String representation, remove the decimal point and everything in front of it, and count the characters of what's left:
d.toString().split('.')[1].length
. Problem is this can quickly give unexpected results because of precision errors with the double type. For example
(0.1 + 0.2).toString().split('.')[1].length
will yield
17
, when it should intuitively be
1
, because of rounding errors. Better to go to using BigDecimals for this kind of job
c
thx
k
Also
Nan
, inifnity, scientific notation, ...
r
Yup, all that too