|
|
Re: trick question about float in java
|
Posted: Jun 1, 2006 4:15 PM
|
|
> > Decimal and BigDecimal use another approach. > They store every digit as a separate value. > So 3.33 Is storeed as [3], [3] [3] with certain formatting > informations. For calculations the value must be converted > to Double of course. The advantage of BigDecimal is, that > you can be sure that the number is exactly what you see on > the screen.
Two Things: 1. BigDecimal technically is not stored as a series of decimal digits. It is actually stored as a BigInteger (which is an array of integers) and an integer representing the location of the decimal place. Storing it as a decimal numbers instead of as binary numbers would be both wasteful and computationally inefficient. However, conceptually it can be thought of as storing each digit, so this is kind of a nit.
2. Converting a BigDecimal to a double for computation would entirely defeat the purpose of using a BigDecimal number. You would loose the precision of BigDecimal during the operation.
Take a look at the source for BigDecimal and BigInteger.
|
|