This week's book giveaways are in the Java EE and JavaScript forums. We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line! See this thread and this one for details.

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.

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

Joined: Dec 23, 2003
Posts: 798

posted

0

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

Joined: Nov 19, 2007
Posts: 269

posted

0

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

Joined: Dec 23, 2003
Posts: 798

posted

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 ?

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

Joined: Nov 19, 2007
Posts: 269

posted

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.