当前位置:网站首页>BigDecimal 进行四舍五入 四舍六入和保留两位小数
BigDecimal 进行四舍五入 四舍六入和保留两位小数
2022-07-28 05:17:00 【wang0112233】
舍入模式向“最近的邻居”舍入,除非两个邻居是等距的,在这种情况下向上舍入。 如果丢弃的分数 ≥ 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);
}
}
}
边栏推荐
- 【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
- Transformer -- Analysis and application of attention model
- Gym 101911c bacteria (minimum stack)
- 自定义Json返回数据
- I interviewed a 38 year old programmer and refused to work overtime
- PC side bug record
- I've been in an outsourcing company for two years, and I feel like I'm going to die
- Classes and objects [medium]
- MySQL practice 45 lectures
- How does Alibaba use DDD to split microservices?
猜你喜欢

Reading notes of SMT practical guide 1

Scope, execution process and life cycle of bean

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

Struct模块到底有多实用?一个知识点立马学习

使用navicat或plsql导出csv格式,超过15位数字后面变成000(E+19)的问题

7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces

多系统架构设计思考

21 day SQL punch in summary

How practical is the struct module? Learn a knowledge point immediately

从微服务基本概念到核心组件-通过一个实例来讲解和分析
随机推荐
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
SimpleDateFormat线程不安全和DateTimeFormatter线程安全
Flask Development & get/post request
Share several methods of managing flag bits in C program
2022 summer practice (first week)
【ARXIV2204】Vision Transformers for Single Image Dehazing
Bean的作用域、执行流程、生命周期
JMeter related knowledge sorting
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
类和对象【中】
Test Development - UI testing in automated testing
【计算机三级信息安全】信息安全保障概述
Database date types are all 0
Autoreleasepool problem summary
HDU 3078 network (lca+ sort)
FreeRTOS个人笔记-任务通知
Offline loading of wkwebview and problems encountered
Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address
【ARXIV2203】SepViT: Separable Vision Transformer
SSLError