Click to See Complete Forum and Search --> : A simple question


orenjikuro
October 21st, 2004, 11:10 AM
Hi guys, i read this off my university java book,
Monetary variables should be held as whole numbers (integers) of pence. $15.99 should be held as 1599 pence. This is to avoid round-off errors when performing arithmetic on these values.

But there isnt any round-off errors when i declare the value as double and use BigDecimal to round it off to two decimal places.

why is declaring it as int a better way?

loverboy260
October 21st, 2004, 11:14 AM
i dunno but if it aint a problem then you should use whatever you prefer!

jw_wvu
October 21st, 2004, 11:16 AM
Are you sure you read your text correctly?

Maybe it said "could" instead of "should".

(Maybe it's an older edition)
(Maybe it hasn't introduced BigDecimal to it's readers yet)


Continue using a BigDecimal. That's what it there for.

orenjikuro
October 21st, 2004, 11:17 AM
Yea, i agree, but its one of the so-called "advice" on how should i do the assignment.

Guess this will affect my grade if i dont follow. Just dont understand why is it better...

jw_wvu
October 21st, 2004, 11:19 AM
If it affects your grade adversly by not following this "advise" then I would find another instructor if I were you.

orenjikuro
October 21st, 2004, 11:22 AM
Haha, well, i had paid the fees....

Guess i can ask my lecturer for more clarification.
Thanks guys anyway!

dlorde
October 21st, 2004, 11:38 AM
Many sources recommend against using floating point variables for monetary representations because of the potential for rounding errors - floating point implementations can only approximate most fractions - in particlar, 0.1 and 0.001 (one tenth and one hundredth). Floating-point statistical averages are ok. But anything that people want tallied with perfect accuracy (ie: most business values) cannot be held in a float or double. See Fixed-point Arithmetic (http://en.wikipedia.org/wiki/Fixed-point_arithmetic) and Floating Point Fractions (http://c2.com/cgi/wiki?FloatingPointFractions).

Furious activity is no substitute for understanding...
H. H. Williams

cjard
October 22nd, 2004, 08:30 PM
and, additionally, you might find that the platform you run java on, doesnt have any hardware support for floats. java enableddd cellphones for example..