# Double rounding

Edward Chen

Ranch Hand

Posts: 798

posted 8 years ago

- 0

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

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

posted 8 years ago

- 0

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

posted 8 years ago

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.

- 0

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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Edward Chen

Ranch Hand

Posts: 798

Bryce Martin

Ranch Hand

Posts: 269

Edward Chen

Ranch Hand

Posts: 798

posted 8 years ago

- 0

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

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

posted 8 years ago

- 0

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.

Bill Shirley - bshirley - frazerbilt.com

if (Posts < 30) you.read( JavaRanchFAQ);

Bryce Martin

Ranch Hand

Posts: 269

posted 8 years ago

- 0

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.

It is sorta covered in the JavaRanch Style Guide. |