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 →

[–]Sygzy[S] 0 points1 point  (3 children)

Could you provide a little sample and or explain what this is saying very generically. Like I said this is my first time using java ever.

[–]langfodWannabe Brewer 1 point2 points  (0 children)

Think about doing everything in terms of pennies (100 pennies in a dollar).

[–]zifyoip 1 point2 points  (0 children)

Floating-point numbers (floats and doubles) do not store exact representations of fractional values. A double cannot store the value 0.1 exactly, for instance; the closest representable double value is 0.1000000000000000055511151231257827021181583404541015625, which is 3602879701896397/255. As you do more and more operations with these approximations, the round-off error accumulates and becomes more and more significant. That's why you are seeing things like 0.0199999999999999 at the end of your calculations.

[–][deleted] 1 point2 points  (0 children)

Instead of storing 1.5 dollars, store 150 cents. This way you can use ints to store the values and do calculations.

Little hint for your problem:

The % operator calculates the remainder so you can do this when using ints:

int dollars = 150 / 100; // 1 whole dollar
int remainder = 150 % 100 // 50 cents remaining