tags:

views:

67

answers:

3

All,

Is there any in built function in java to tell me how many decimal places in a double. For example:

101.13 = 2 101.130 = 3 1.100 = 3 1.1 = 1 -3.2322 = 4 etc

I am happy to convert to another type first if needed, I have looked at converting to bigdecimal first with no luck.

Any help would be appreciated.

Cheers, Karl

A: 

The number of decimal places in a double is 16.

64-bit numbers. 52-bit Mantissa. 52 bits is about 16 decimal digits.

See http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html.

double, whose values include the 64-bit IEEE 754 floating-point numbers.

See http://en.wikipedia.org/wiki/IEEE_754-2008

S.Lott
No, that's the number of decimal *digits,* and it's wrong, it is 15.9. Consider "123456789012345". Fifteen decimal digits, zero decimal places. "12345678901234.5". Fifteen decimal digits, one decimal place. Etc.
EJP
@EJP: I would think that 15.9 is "About 16". It appears from your comment that you don't think 15.9 is "about 16". If you want to claim 15.9 is not "about 16", then you're wrong also, since it's not 15.9, either. Wikipedia says "17 decimal digits" and 15.955 digits.
S.Lott
Then both you and Wikipedia are wrong. When it comes to decimal digits, 15.9 is about *15,* because you can't rely on the 16th digit being there. And it certainly isn't 17.
EJP
@EJP: Please read the IEEE standard carefully. In particular, read about the implicit bits in the significand.
S.Lott
There are 53 bits of mantissa. That's 53 binary digits, which in turn is 15.9 decimal digits. Some of the time you get 16; most ofmthe time don't. If you are implementing to a precision of 16 decimal digits, 15.9 isn't enough.
EJP
@EJP: "15.9 decimal digits" is wrong. It's 15.955 digits.
S.Lott
OK, we are just quibbling here, but it is 15.9545897701910000. To 16 decimal places ;-)
EJP
@EJP: No. We're not quibbling. Your initial comment was clear. "about 16 is wrong". And then you claimed it was 15.9 which is equally wrong. You don't appear to he quibbling. You appear to have something important to say and you've repeated that something important now four separate times. That's not quibbling. You've got something **important** to add to this answer. Something about 16 being not even close to 15.955.
S.Lott
OK. *I* am quibbling.
EJP
+5  A: 

No.

1.100 and 1.1 are exactly the same value (they are represented exactly the same bit-for-bit in a double).

Therefore you can't ever get that kind of information from a double.

The only thing you can do is to get the minimum number of decimal digits necessary for a decimal number to be parsed into the same double value. And that is as easy as calling Double.toString() and checking how many decimal digits there are.

Joachim Sauer
Calling toString() seems a little shakey but was my initial thought.
Karl
@Karl: **it is shaky**, but possibly the best thing you can do. It breaks for very large and very small values (as you'll get scientific notation). The only 100% correct solution is to do that test when you have the value as a `String` **before** you convert it to a `double` (as Andrei demonstrates).
Joachim Sauer
+4  A: 

You could use BigDecimal.scale() if you pass the number as a String like this:

BigDecimal a = new BigDecimal("1.31");
System.out.println(a.scale()); //prints 2
BigDecimal b = new BigDecimal("1.310");
System.out.println(b.scale()); //prints 3

but if you already have the number as string you might as well just parse the string with a regex to see how many digits there are:

String[] s = "1.31".split("\\.");
System.out.println(s[s.length - 1].length());

Using BigDecimal might have the advantage that it checks if the string is actually a number; using the string method you have to do it yourself. Also, if you have the numbers as double you can't differentiate between 1.31 and 1.310 (they're exactly the same double) like others have pointed out as well.

Andrei Fierbinteanu