pounds = pennies/100

E.g. 400p/100 = £4

450p/100 = £5

When I do this, Java always rounds down ignoring the decimal value because it uses integer division. My textbook says I should solve this by "adding a value to the numerator before dividing by 100". I've thought about this for a few days, and I can't think what the value can be. It's irritating me as I cannot progress further in the course until I complete this project - does anyone know what this value could be and have an explanation of why?

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

So what if instead of 100 pennies per pound, it were only 10? you can now easily run through all the cases in your head...

1 penny == 0£

2 penny == 0£

3 penny == 0£

..etc

at what point do we flip from 0£ to 1£? from 1£ to 2£? See if you can recognize a pattern.

Test a few cases. If it holds, try and extrapolate to your 100 pennies case...if not, go back and play around some more.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie wrote:I mistakenly edited your post because I thought it was somebody else giving a complete solution, which you shouldn't do. Sorry I have restored the post from our history files.

So is there a rule to make people try to work things out for themselves as much as possible?

If so, that's great because I can ask for help without feeling bad for doing it!

`BigDecimal`:

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

It really depends on what your criteria are. If you consider an even hundred to be exact, then there are 99 other values that must be rounded (1 through 99). By adding 50 and then dividing, then there are 49 values that will round down, and 50 values that will round up.

One standard way to "unbias" this result is to round one-half upward if the integer part if odd, and downward if the integer part is even ( or the other way around, if you wish).

Thus 150 pennies rounds up to 2 pounds, while 450 pounds rounds down to 4 pounds.

Of course, there are also people who will claim this method is also biased.

Jason Rose wrote:Campbell Ritchie wrote:I mistakenly edited your post because I thought it was somebody else giving a complete solution, which you shouldn't do. Sorry I have restored the post from our history files.

So is there a rule to make people try to work things out for themselves as much as possible?

If so, that's great because I can ask for help without feeling bad for doing it!

Yes, in the Beginning Java forum, we try to help you arrive at your own solution and complete solutions are often removed until such time as the OP has posted their own solution.

All things are lawful, but not all things are profitable.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]