By Alvin Alexander. Last updated: May 11, 2018
As a quick note, here’s a Java method that will round a float to the nearest half value, such as 1.0, 1.5, 2.0, 2.5, etc.:
/**
* converts as follows:
* 1.1 -> 1.0
* 1.3 -> 1.5
* 2.1 -> 2.0
* 2.25 -> 2.5
*/
public static float roundToHalf(float f) {
return Math.round(f * 2) / 2.0f;
}
The comments show how this function converts the example float values to their nearest half value, so I won’t add any more details here. I don’t remember the origin of this algorithm — I just found it in some old code, thought it was clever, and thought I’d share it here.

