Skip to content

Instantly share code, notes, and snippets.

@christianascone
Created July 12, 2017 13:55
Show Gist options
  • Save christianascone/355636f0e8c3b0f0e184a2cb4a930115 to your computer and use it in GitHub Desktop.
Save christianascone/355636f0e8c3b0f0e184a2cb4a930115 to your computer and use it in GitHub Desktop.
Java utils for a few (less used) methods used for number rounding
/**
* Util class for number rounding
*
* @author Christian Ascone
*
*/
public class MathRoundUtil {
/**
* Rounds a double number to the closest 0.5 divisor.
* For example:
* 2 -> 2
* 2.1 -> 2
* 2.3 -> 2.5
* 2.5 -> 2.5
* @param toRound Number to round
* @return The closest 0.5 divisor of given number
*/
public static double roundToClosestHalf(double toRound) {
double f = 0.5;
double rounded = f * Math.round(toRound / f);
return rounded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment