Number of decimal digits in a double [closed]

A double is not always an exact representation. You can only say how many decimal places you would have if you converted it to a String.

double d= 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;

This will only work for numbers which are not turned into exponent notation. You might consider 1.0 to have one or no decimal places.

Leave a Comment