programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# How do I round pennies to pounds in Java?

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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?

Sheriff
Posts: 16040
266
• Number of slices to send:
Optional 'thank-you' note:
Think: at what point should it round up? Try using that point to add to the actual value and see what results you get.

lowercase baba
Posts: 12955
65
• Number of slices to send:
Optional 'thank-you' note:
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.

Junilu Lacar
Sheriff
Posts: 16040
266
• Number of slices to send:
Optional 'thank-you' note:
...and Welcome to the Ranch!

Jason Rose
Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
Would the formula Pounds = (Pence + 50)/100 do it?

Marshal
Posts: 71722
312
• Number of slices to send:
Optional 'thank-you' note:

Jason Rose wrote:Would the formula . . . do it?

Yes

And welcome again

Jason Rose
Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:

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!

fred rosenberger
lowercase baba
Posts: 12955
65
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:

Jason Rose wrote:Would the formula . . . do it?

Yes

And welcome again

pfff...i would have replied with "what happened when you tested it???"

Campbell Ritchie
Marshal
Posts: 71722
312
• Number of slices to send:
Optional 'thank-you' note:

fred rosenberger wrote:. . . pfff...i would have replied with "what happened when you tested it???"

Surely that's, “What happened when you tested it after I mistakenly deleted it ?”

Saloon Keeper
Posts: 12622
273
• Number of slices to send:
Optional 'thank-you' note:
Another way to do this is with BigDecimal:

Bartender
Posts: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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.

Campbell Ritchie
Marshal
Posts: 71722
312
• Number of slices to send:
Optional 'thank-you' note:

Fred Kleinschmidt wrote:. . . Of course, there are also people who will claim this method is also biased.

Surely what the OP wants is a simple way to program rounding.

Stephan van Hulst
Saloon Keeper
Posts: 12622
273
• Number of slices to send:
Optional 'thank-you' note:
Fred's point is that you first need to determine what kind of rounding you want.

Bartender
Posts: 4272
160
• Number of slices to send:
Optional 'thank-you' note:
Whatever rounding rule is chosen, OP better hurry. Next year, after the Brexit, no doubt the English will reintroduce the haypenny,  12 shillings in a pound, ....  Now that will bring complex rounding rules.

Campbell Ritchie
Marshal
Posts: 71722
312
• Number of slices to send:
Optional 'thank-you' note:
We never had twelve shillings in the pound. Rampant inflation in the 60s and 70s just made it feel like that.

And I know the Weimar Republic had inflation many times worse.

Sheriff
Posts: 7111
184
• Number of slices to send:
Optional 'thank-you' note:

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.

Fred Kleinschmidt
Bartender
Posts: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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: 71722
312
• Number of slices to send:
Optional 'thank-you' note:

Fred Kleinschmidt wrote:. . . there have been cases where the biased roundoff advantage was deposited into the programmer's account.

So that happens in real life as well as in films?

Stephan van Hulst
Saloon Keeper
Posts: 12622
273
• Number of slices to send:
Optional 'thank-you' note:
The films are likely based on the real thing.

Junilu Lacar
Sheriff
Posts: 16040
266
• Number of slices to send:
Optional 'thank-you' note:
It's called "salami slicing" or "penny shaving"

 Consider Paul's rocket mass heater.