2

I'd like make my program to be even more precise and to consider that there aren't coins for 1,2,7 values (only 5,10,20,50,100..), so there is a rule called rounding to five forints.

Example:

111,112  -> 110 
113,114  -> 115
116,117  -> 115
118,119  -> 120

My question is which function should I use in JAVA to reach my goal and get the right rounded value?

glglgl
  • 89,107
  • 13
  • 149
  • 217
Bence Szabari
  • 143
  • 1
  • 2
  • 12

4 Answers4

6

You could write it this way:

int roundTo5(int value)
{
    return ((value + 2) / 5) * 5;
}

This takes any forint value, adds 2 and divides by 5. This makes a "floor rounding" of integers and multiplies 5 again.

0 → 2 → 0 → 0
1 → 3 → 0 → 0
2 → 4 → 0 → 0
3 → 5 → 1 → 5
4 → 6 → 1 → 5
5 → 7 → 1 → 5
6 → 8 → 1 → 5
7 → 9 → 1 → 5
8 → 10 → 2 → 10
9 → 11 → 2 → 10
glglgl
  • 89,107
  • 13
  • 149
  • 217
1

you can use following method.

public static int roundTo5(int n) {
        int t = n % 10; // get modulo 10 of n
        switch (t) {
        case 0:
        case 1:
        case 2:
            // result will be n - n%10;
            return n - t;
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            // result will be n - n %10 + 5
            return n - t + 5;
        case 8:
        case 9:
            // result will be n - n%10 + 10;
            return n - t + 10;
        }
        return 0;
}
Naim
  • 86
  • 5
  • Whilst this isn't concise, it's extremely readable and provably correct. +1 – Ryan Leach Jun 06 '18 at 08:31
  • But could be specified as %5 instead. – Ryan Leach Jun 06 '18 at 08:37
  • 1
    thanks Ryan for the suggetion. I have modified the method to public static int roundTo5(int n) { int t = n % 5; // get modulo 5 of n switch (t) { case 0: case 1: case 2: // result will be n - n%5 ; return n - t; case 3: case 4: // result will be n - n%5 + 5 return n - t + 5; } return 0; } . This also provides the same result. – Naim Jun 06 '18 at 08:50
0

Say a is your integer or float you want to round :

int round = Math.round( ((float)(a)/5) * 5);

if you declare as a float you can skip the type casting.

  • This will give a value of 0 for 4 (if `a` is integer). Also, there is an extra parenthesis. – RealSkeptic Jun 06 '18 at 08:24
  • ay yeah. done my testing with float numbers. a typecasting will do. – Fillipos Christou Jun 06 '18 at 08:43
  • But it is quite a bad idea to use floats for currency values at the first place. – glglgl Jun 06 '18 at 08:54
  • next time you buy a $7.67 product, you tell this to the cashier. xD – Fillipos Christou Jun 06 '18 at 09:04
  • @FilliposChristou that is still an integer number (of cents) though. Prices that are really more precise than the smallest coin for a currency are rare (one popular example are petrol prices here in Europe), but afaik the total still gets calculated using fixed-point math – Hulk Jun 06 '18 at 09:15
0

I don't see why this wasn't mentioned, but the following seems that should do the job well:

5 * (Math.round(n / 5))
NiVeR
  • 9,644
  • 4
  • 30
  • 35