• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double rounding

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks for your attention.

 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic