This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with BigDecimal and Double

 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need accuracy you want to avoid double and use BigDecimal.
 
shiva viswanathan
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic