概述
舍入模式向“最近的邻居”舍入,除非两个邻居是等距的,在这种情况下向上舍入。 如果丢弃的分数 ≥ 0.5,则与 ROUND_UP 一样; 否则,行为与 ROUND_DOWN 相同。 请注意,这是我们大多数人在小学时所教的舍入模式。
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
舍入模式向“最近的邻居”舍入,除非两个邻居是等距的,在这种情况下向下舍入。 如果丢弃的分数 > 0.5,则表现与 ROUND_UP 相同; 否则,行为与 ROUND_DOWN 相同。
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal >} 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5;
保留小数位
返回一个 BigDecimal,其标度为指定值,其未标度值通过将此 BigDecimal 的未标度值乘以或除以适当的 10 次方来确定,以保持其整体值。 如果通过操作缩小比例,则必须将未缩放的值除(而不是相乘),并且值可能会改变; 在这种情况下,指定的舍入模式应用于除法。
/**
* Returns a {@code BigDecimal} whose scale is the specified
* value, and whose unscaled value is determined by multiplying or
* dividing this {@code BigDecimal}'s unscaled value by the
* appropriate power of ten to maintain its overall value. If the
* scale is reduced by the operation, the unscaled value must be
* divided (rather than multiplied), and the value may be changed;
* in this case, the specified rounding mode is applied to the
* division.
*
* <p>Note that since BigDecimal objects are immutable, calls of
* this method do <i>not</i> result in the original object being
* modified, contrary to the usual convention of having methods
* named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
* Instead, {@code setScale} returns an object with the proper
* scale; the returned object may or may not be newly allocated.
*
* <p>The new {@link #setScale(int, RoundingMode)} method should
* be used in preference to this legacy method.
*
* @param newScale scale of the {@code BigDecimal} value to be returned.
* @param roundingMode The rounding mode to apply.
* @return a {@code BigDecimal} whose scale is the specified value,
* and whose unscaled value is determined by multiplying or
* dividing this {@code BigDecimal}'s unscaled value by the
* appropriate power of ten to maintain its overall value.
* @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
* and the specified scaling operation would require
* rounding.
* @throws IllegalArgumentException if {@code roundingMode} does not
* represent a valid rounding mode.
* @see #ROUND_UP
* @see #ROUND_DOWN
* @see #ROUND_CEILING
* @see #ROUND_FLOOR
* @see #ROUND_HALF_UP
* @see #ROUND_HALF_DOWN
* @see #ROUND_HALF_EVEN
* @see #ROUND_UNNECESSARY
*/
public BigDecimal setScale(int newScale, int roundingMode) {
if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
throw new IllegalArgumentException("Invalid rounding mode");
int oldScale = this.scale;
if (newScale == oldScale) // easy case
return this;
if (this.signum() == 0) // zero can have any scale
return zeroValueOf(newScale);
if(this.intCompact!=INFLATED) {
long rs = this.intCompact;
if (newScale > oldScale) {
int raise = checkScale((long) newScale - oldScale);
if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) {
return valueOf(rs,newScale);
}
BigInteger rb = bigMultiplyPowerTen(raise);
return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
} else {
// newScale < oldScale -- drop some digits
// Can't predict the precision due to the effect of rounding.
int drop = checkScale((long) oldScale - newScale);
if (drop < LONG_TEN_POWERS_TABLE.length) {
return divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode, newScale);
} else {
return divideAndRound(this.inflated(), bigTenToThe(drop), newScale, roundingMode, newScale);
}
}
} else {
if (newScale > oldScale) {
int raise = checkScale((long) newScale - oldScale);
BigInteger rb = bigMultiplyPowerTen(this.intVal,raise);
return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
} else {
// newScale < oldScale -- drop some digits
// Can't predict the precision due to the effect of rounding.
int drop = checkScale((long) oldScale - newScale);
if (drop < LONG_TEN_POWERS_TABLE.length)
return divideAndRound(this.intVal, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode,
newScale);
else
return divideAndRound(this.intVal, bigTenToThe(drop), newScale, roundingMode, newScale);
}
}
}
使用除法保留三位小数divide
b.discounts().divide(BigDecimal.TEN, 3, RoundingMode.HALF_UP)
最后
以上就是害怕小懒猪为你收集整理的BigDecimal 进行四舍五入 四舍六入和保留两位小数,三位四位小数的全部内容,希望文章能够帮你解决BigDecimal 进行四舍五入 四舍六入和保留两位小数,三位四位小数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复