Created
April 1, 2013 13:49
-
-
Save stden/5285052 to your computer and use it in GitHub Desktop.
Округление до нужного числа знаков
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
/** | |
* Округление сумм и другие операции с бонусами | |
*/ | |
public class BonusUtil { | |
/** | |
* Округление суммы до двух знаков после запятой. | |
* По правилам математики (к ближайшему). | |
* | |
* @param sum Сумма | |
* @return Округлённая сумма | |
*/ | |
public static BigDecimal round(BigDecimal sum) { | |
// Умножаем на 100 - получаем сумму в копейках | |
BigDecimal value = sum.multiply(BigDecimal.valueOf(100)); | |
// Целое округлённое число копеек | |
long intVal = value.add(BigDecimal.valueOf(0.5)).longValue(); | |
// Преобразуем обратно в BigDecimal | |
return BigDecimal.valueOf(intVal, 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment