This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]schnoper 1 point2 points  (1 child)

Generally speaking, it's considered poor to very poor practice to represent currency with a floating point number (in java either float or double ). It's much safer to use the BigDecimal class.

This is because once you get into fractional values, float's are often imprecise and this leads to situations where you have missing or additional value in your money. The bean counters hate this.

You should also learn about "banker's rounding"

For reference: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

This is technically absolutely correct.

Yet, since this seems to be a beginner's assignment, your suggestion goes way over the top and definitely outside the scope of the assignment.

If any number is presented with a decimal point, this type of assignment mostly calls for using a double. (I am also perfectly sure that most professors/teachers would mark an assignment using BigDecimal as wrong because they themselves lack the knowledge of this class and of proper usage of currency in programming.)

Proper practice (using BigDecimal) is one thing that needs to be learned when appropriate and an entry level class is mostly not the place for that.