Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of decimal digits in a double [closed]

Tags:

java

double

How do I determine number of integer digits and the number of digits after decimal in a number like 234.12413 in Java.

like image 709
Anshul Avatar asked Jun 07 '11 11:06

Anshul


People also ask

Can a double store a decimal?

Float and double are both widely used data types in programming that have the ability to store decimal or floating-point​ numbers.

How many decimal places can a double hold C?

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.

How do you limit a double to two decimal places?

DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places.


1 Answers

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.

like image 123
Peter Lawrey Avatar answered Sep 29 '22 09:09

Peter Lawrey