• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to avoid exponential form  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am having a problem handling a double-type variable. We actually create a flat file as per specifications, from the database records.

When we designed this module, we did not expect this variable to confront such a big value. We query the database, populate a recordset object and this then assign value to the variable by calling:

recordset.getdouble(columnname) method.

Now, suddenly we have got a value which is 11 million (11667500.00) While debugging I saw that it is being stored as 1.1667E7 (as an exponential). so when we parse it (we actually split left (integer) and right (decimals) part using "." as splitter and force a decimal of two places, after rounding using some string converions and manipulations). So, $11667500.00 should appear as 11667750000.

However, because of exponential form being stored in this variable, we are getting 117 after parsing, because number is parsed using 1.1667E7 as input.

Should I use BigDecimal for the variable type to hold value as 116677500.00 (not 1.16677E7)?
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing the data (just a number) with its possible representations ('1.16675E7' or '11667500' and so on). None of those representations are stored in a "double" variable, only the number.

When you look at the number, Java converts it to a string in some way. If you allow Java to choose how to format the number, you get one way of formatting it. If you don't like how it's formatted, you are free to format it in some other way. You would (and should) use the DecimalFormat class to do that.
 
Mahesh Sethi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for help
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Paul C]: If you don't like how it's formatted, you are free to format it in some other way. You would (and should) use the DecimalFormat class to do that.

Mahesh - if you're using JDK 5 you can also use a format specifier, documented in the Formatter class and available through String.format(), System.out.format(), and elsewhere. For example:

This formats a floating-point number to take up 16 chars total, with 2 of them after the decimal.

Since you're dealing with money, I would recommend not using double to store the values at all. Floating-point arithmetic may result in roundoff error more easily. Typically I would instead use an int or long which represents the number of cents. Some applications may require you to caluculate in tenths of a cent instead. Alternately you may indeed wish to use BigInteger or BigDecimal to be able to store arbitrarily large numbers without any rounding. It may also be worthwhile to create a new wrapper class which represents an amount of money, and interally uses an int or long or BigDecimal or whatever. The key is to hide this decision from other classes as much as possible, so if you later find that you need to change it, you can just change it in your wrapper class, and other code doesn't need to know. Worth thinking about, especially if you're dealing with money in more than one class already...
[ December 02, 2005: Message edited by: Jim Yingst ]
 
Mahesh Sethi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. I am now using BigDecimal for this variable. Now I created an second (overloaded) method for parsing the money amount, with parameter as BigDecimal instead of double.

This was very rare situation (a previous CEO sold his stock options for $11 million in one day.. ). Normally the amounts does not go over a 100k, (at most). So, could never exploded before
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!