How do I determine number of integer digits and the number of digits after decimal in a number like 234.12413
in Java.
Float and double are both widely used data types in programming that have the ability to store decimal or floating-point numbers.
Doubles: double double takes double the memory of float (so at least 64 bits). In return, double can provide 15 decimal place from 2.3E-308 to 1.7E+308.
DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With