22

How is this possible in Java?

I have a float and I'd like to round it to the nearest .5.

For example:

1.1 should round to 1.0

1.3 should round to 1.5

2.5 should round to 2.5

3.223920 should round to 3.0

EDIT: Also, I don't just want the string representation, I want an actual float to work with after that.

user1513171
  • 1,912
  • 6
  • 32
  • 54
  • 7
    Multiply by two, round and finally divide by two – Sami Korhonen May 03 '14 at 20:51
  • I tried countless different inputs in Google and nothing gave me that post. – user1513171 May 03 '14 at 20:53
  • 1
    @user1513171 I just googled: [round to nearest 0.5 java](https://www.google.com.mx/search?q=round+to+nearest+0.5+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:es-MX:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=91ZlU9jaN6iR8QfP4YD4Dw) that way and gave me so many posts from SOF – Frakcool May 03 '14 at 20:56
  • I googled this: https://www.google.com/search?q=round+to+nearest+0.5+java&ie=utf-8&oe=utf-8&rls=org.mozilla:es-MX:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=91ZlU9jaN6iR8QfP4YD4Dw#channel=sb&q=round+to+nearest+.5+java&rls=org.mozilla:es-MX:official But whatever. Thanks for the answer. – user1513171 May 03 '14 at 20:57
  • Google ignores punctuation, so you were essentially searching for 'round to the nearest 5 in java' : https://support.google.com/websearch/answer/2466433?hl=en – Mardoz May 03 '14 at 21:00

3 Answers3

46

@SamiKorhonen said this in a comment:

Multiply by two, round and finally divide by two

So this is that code:

public static double roundToHalf(double d) {
    return Math.round(d * 2) / 2.0;
}

public static void main(String[] args) {
    double d1 = roundToHalf(1.1);
    double d2 = roundToHalf(1.3);
    double d3 = roundToHalf(2.5);
    double d4 = roundToHalf(3.223920);
    double d5 = roundToHalf(3);

    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);
    System.out.println(d4);
    System.out.println(d5);
}

Output:

1.0
1.5
2.5
3.0
3.0
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
10

The general solution is

public static double roundToFraction(double x, long fraction) {
    return (double) Math.round(x * fraction) / fraction;
}

In your case, you can do

double d = roundToFraction(x, 2);

to round to two decimal places

double d = roundToFraction(x, 100);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    What if I want to round to nearest .75? Would I pass 4/3 instead of 2? Thanks – theprogrammer Jan 23 '20 at 16:52
  • @theprogrammer that would be the nearest multiple of 0.75, e.g. 1.5, 2.25, 3.00. If you wanted the nearest number ending in 0.75, subtract 0.75, round it and add 0.75 back. – Peter Lawrey Feb 13 '20 at 18:40
4

Although it is not that elegant, you could create your own Round function similarly to the following (it works for positive numbers, it needs some additions to support negatives):

public static double roundHalf(double number) {
    double diff = number - (int)number;
    if (diff < 0.25) return (int)number;
    else if (diff < 0.75) return (int)number + 0.5;
    else return (int)number + 1;
}
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24