Created
July 12, 2017 13:55
-
-
Save christianascone/355636f0e8c3b0f0e184a2cb4a930115 to your computer and use it in GitHub Desktop.
Java utils for a few (less used) methods used for number rounding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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