Jason Rose

Greenhorn

Posts: 3

posted 4 months ago

I'm currently doing a beginner Java course, and my latest project is to make a simple program that rounds pennies to the nearest pound. The formula I'm using is..

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?

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?

posted 4 months ago

Think: at what point should it round up? Try using that point to add to the actual value and see what results you get.

*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]

posted 4 months ago

There is a great book out called "How to Solve It", by George Pólya. It gives you several techniques to try when you are stumped, one of which is "try a simpler case".

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.

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

posted 4 months ago

...and Welcome to the Ranch!

*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]

Jason Rose

Greenhorn

Posts: 3

Campbell Ritchie

Marshal

Posts: 56599

172

Jason Rose

Greenhorn

Posts: 3

posted 4 months ago

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!

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!

Campbell Ritchie

Marshal

Posts: 56599

172

Stephan van Hulst

Saloon Keeper

Posts: 7993

143

Fred Kleinschmidt

Bartender

Posts: 572

9

posted 4 months ago

Adding 50 to the number of pennies before the integer division is one way to do it, but many mathematicians would claim that to be biased.

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.

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.

Campbell Ritchie

Marshal

Posts: 56599

172

Stephan van Hulst

Saloon Keeper

Posts: 7993

143

Piet Souris

Master Rancher

Posts: 2044

75

Campbell Ritchie

Marshal

Posts: 56599

172

posted 4 months ago

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.

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.

Fred Kleinschmidt

Bartender

Posts: 572

9

posted 4 months ago

When working with money, it is very important to choose the proper rounding algorithm. This is crucial if you are writing code for a bank or other financial institution Bank officials have been prosecuted for using rounding techniques that hide minute biases in the bank's favor. Also there have been cases where the biased roundoff advantage was deposited into the programmer's account.

Campbell Ritchie

Marshal

Posts: 56599

172

Stephan van Hulst

Saloon Keeper

Posts: 7993

143

posted 4 months ago

It's called "salami slicing" or "penny shaving"

*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]