aspose file tools*
The moose likes Java in General and the fly likes Trouble with BigDecimal and Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trouble with BigDecimal and Double" Watch "Trouble with BigDecimal and Double" New topic
Author

Trouble with BigDecimal and Double

shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi All ,

I am having trouble with a double number . I have a number in DB with 15 digits before decimal point and 3 digits after the decimal

I need to display it to user on GUI

When i convert it to String then it is shown in exponential form
with some truncation .

I tried some statements with BigDecimal , DecimalFormat and
NumberFormat but no luck

Can anyone suggest how i can print this

ie
i have double d = 999999999999999.999;

When i print it i should get the same value .
Can anyone suggest some way ?

Thanks,
Shiva
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

I have some questions:

1. What type is the column of data in the database (and what database)?
2. How are you retrieving it?

My suggestion would be something like:

shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi ,

The problem is not with DB . Once i get it into double var , i am not
able to format it and display it in the format 15,3

Can you suggest some specific code using BigDecimal or something since even using BigDecimal the last digit is truncated

Thanks
Shiva
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

You still don't say what the database column type is for the data you are retrieving.

Anyway, you could try something like this (untested):

Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
If you need accuracy you want to avoid double and use BigDecimal.
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
exactly even with Big Decimal the number is getting truncated

I have tried some combinations of BigDecimal but no luck

Can you tell me exact code for doing this assuming the no of places after the decimal is 3 and before is 15

here is the code i tried
double d1 = 999999999999999.999;
String s = "" + d1;
System.out.println(d1);
System.out.println(s);

BigDecimal bigDecimal = new BigDecimal(s);
bigDecimal.setScale(3,BigDecimal.ROUND_HALF_UP);
System.out.println(bigDecimal.scale( ));
System.out.println(bigDecimal);

DecimalFormat df = new DecimalFormat("###############.###");
System.out.println("Formatted double: " + df.format(d1));

and the output

1.0E15
1.0E15
0
1000000000000000
Formatted double: 1000000000000000

Please suggest the exact code .
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Right, the problem with the code you show is that you are assigning the literal to the double variable d1, then using that to create the BigDecimal!

Of course, doing this, the rounding error from the double is passed on to the BigDecimal. By the way, it's not being truncated, it's rounding a rounding error up to 1000000000000000.

You need to read the value from your database directly into a BigDecimal using:

BigDecimal myVar = resultSet.getBigDecimal("column name");

Try this example:



With the result:

double value: 1000000000000000.0
BigDecimal value: 999999999999999.999
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi Horatio ,

First of all , thanks a ton for patiently answering my questions :-)

I want to clarify this suppose i do this
double d = resultSet.getDouble(someFieldName);

and in the Db the number is 999999999999999.999 (15,3)

you mean to say the double will not be able to hold the precision

I thought that double has a much higher range .


The problem for me is that the code which retreives from the Db belongs
to another project and i cant ask them to change code. Morover the field is a double field in informix

Suppose i have access to this double and then i use the BigDecimal part is there no way to maintain accuracy and print the value as 999999999999999.999

Also note that in my sample code i am converting the double to string then using it in constructor of bigdecimal does even this not make a difference
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A Java double number has three parts: 52 bits of "significand" and 11 bits of "exponent" and a 1 bit sign. When a number is too big for the 52 bit significand the exponent kicks in to multiply (or divide) the significand by 10, 100, 1000 etc. Any time you multiply you get zeros for the rightmost digits, losing some precision. In most domains (except Bill Gates' checking account!) when you have a number in the trillions, the ones don't make that much difference.

I have no idea what the structure of an Informix double is but it's probably similar. If so, the precision you are looking for may simply not be there.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trouble with BigDecimal and Double