本文整理汇总了Java中java.math.RoundingMode.UP属性的典型用法代码示例。如果您正苦于以下问题:Java RoundingMode.UP属性的具体用法?Java RoundingMode.UP怎么用?Java RoundingMode.UP使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类java.math.RoundingMode
的用法示例。
在下文中一共展示了RoundingMode.UP属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: roundPriceToTickSize
/**
* @param price price to be rounded, has to be positive
* @param roundingMode rounding mode that should be used (only {@link RoundingMode#UP} and {@link RoundingMode#DOWN}
* are supported)
* @return price rounded to tick size
*
* @throws IllegalArgumentException if {@code roundingMode} is neither {@link RoundingMode#UP} nor
* {@link RoundingMode#DOWN} or {@code price} is not positive
*/
static BigDecimal roundPriceToTickSize(final BigDecimal price, final RoundingMode roundingMode, final BigDecimal tickSize) {
checkArgument(
roundingMode == RoundingMode.UP || roundingMode == RoundingMode.DOWN,
"Only rounding UP or DOWN supported"
);
checkArgument(price.compareTo(BigDecimal.ZERO) >= 0, "price=%s < 0", price);
final BigDecimal remainder = price.remainder(tickSize);
if (remainder.compareTo(BigDecimal.ZERO) == 0) {
return price;
}
final BigDecimal result = price.subtract(remainder);
if (roundingMode == RoundingMode.UP) {
return result.add(tickSize);
} else {
return result;
}
}