posted 12 years ago

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

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

shiva viswanathan

Ranch Hand

Posts: 152

Horatio Westock

Ranch Hand

Posts: 221

shiva viswanathan

Ranch Hand

Posts: 152

posted 12 years ago

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 .

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

posted 12 years ago

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

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

posted 12 years ago

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

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

posted 12 years ago

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.

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

Consider Paul's rocket mass heater. |