• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I round pennies to pounds in Java?  RSS feed

 
Jason Rose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think: at what point should it round up? Try using that point to add to the actual value and see what results you get.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and Welcome to the Ranch!
 
Jason Rose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would the formula Pounds = (Pence + 50)/100 do it?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Rose wrote:Would the formula . . . do it?
Yes

And welcome again
 
Jason Rose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?”
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do this is with BigDecimal:
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred's point is that you first need to determine what kind of rounding you want.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The films are likely based on the real thing.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called "salami slicing" or "penny shaving"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!