Win a copy of The Java Performance Companion this week in the Performance forum!

# Double rounding

Edward Chen
Ranch Hand
Posts: 798
I am stuck with the Double rounding.

My goal is to convert a double to string without rounding.

we found the rounding is related to the integer, why ? what is the maximum of the decimal digits, 16 or 15 ? what is the rule here?

Thanks

Bryce Martin
Ranch Hand
Posts: 269
How are you converting from double to string? You could create your double using a NumberFormat. Then you can specify the number of decimal places and then when you do the .toString() it shouldn't round it.

Peter Chase
Ranch Hand
Posts: 1970
Originally posted by Edward Chen:

we found the rounding is related to the integer, why ? what is the maximum of the decimal digits, 16 or 15 ? what is the rule here?

Primitive numbers (double, int, float etc.) are not stored in decimal. They are stored in binary. Therefore, there is not an exact number of decimal digits available. It is around about 15 digits, for a double.

However, you should never rely on the values of the last couple of digits of a floating-point number. Floating-point is not designed to give total accuracy; it's designed to store numbers of widely-varying magnitude, within a fixed, smallish amount of memory, and to allow fast calculation.

It is important to realise that when you assign a literal (e.g. 1.23456) to a floating-point number, it may not store that exact value. That is true even if the number of decimal digits is much less than the number of digits stored by that type.

If you want total accuracy, use BigDecimal. Be aware that it uses more memory than floating-point and is slower.

Alternatively, if your numbers are all of similar magnitude, you could use ordinary integers (e.g. long) to store them, with an implicit multiplier. The classic example of this is currency, where you store everything in integer numbers of cents, not decimal (or, much worse, floating-point) amounts of dollars.

Edward Chen
Ranch Hand
Posts: 798
Unfortunately it has rounding.

Actually d3 conversion is what I want. what I want is no rounding, just delete those extra digits. Here, for d1, I want to get 8.1234567890123456; for d2, I want to get 1.1234567890123456.

Bryce Martin
Ranch Hand
Posts: 269
Try the Big Decimal that was suggested. If the processing time is acceptable then you're set. If not, check back, or look for examples of doing the integer calculations. There are lots of examples around.

Edward Chen
Ranch Hand
Posts: 798
Sorry, Double and BigDecimal can't generate what I want.

How to do integer calculation ? Could you please give me more detail or get me a link? Do you mean, 8.1234567890123456, I firstly change it to 81234567890123456 ?

Thanks

Bill Shirley
Ranch Hand
Posts: 457
The short version of Peter's answer: floating point number storage on computers gets very deep into Computer Science arcana. The answer to your question is the good subject for over a weeks detailing in a CS class. It's a similar problem in many/most computer languages.

Bryce Martin
Ranch Hand
Posts: 269
If you know that you want 15 decimal places then multiply the number by 10^15 and put that into an integer. Then you can store it as a string. You can even insert the decimal point wherever you want in the string. If you know its 15 decimal places then you can put it in the position (string.length() - 15) Then you will get your decimal number. Its kind of long but it will work.