首页 > 编程知识 正文

bigdecimal除法取整,bigdecimal保留5位小数

时间:2023-05-03 17:18:30 阅读:146045 作者:3912

用BigDecimal的divide法进行除法运算时,如果不能被整除,出现无限循环小数,就会抛出异常的东西。 异常如下。 Java.lang.arithmetic exception : non-terminatingdecimalexpansion; noexactrepresentabledecimalresult.at Java.math.bigdecimal.divide (未知资源) )。 解决方案:在divide中输入正确的小数点divide (xxxxx

,2,BigDecimal.ROUND_HALF_EVEN )

BigDecimal定义了舍入模式,仅在进行除法和四舍五入时使用。 以下简单说明。 有关详细信息,请参阅J2se API文档

静态int

ROUND_CEILING

roundingmodetoroundtowardspositiveinfinity。

向正的无限方向舍入

静态int

ROUND_DOWN

轮调模式到轮调towards zero。

向零方向舍入

静态int

ROUND_FLOOR

roundingmodetoroundtowardsnegativeinfinity。

向负的无限方向舍入

静态int

ROUND_HALF_DOWN

roundingmodetoroundtowards ' nearest neighbor ' unlessbothneighborsareequidistant,in which case round down。

除非两侧距离相等,否则四舍五入到最接近(距离)的一侧。 如果是,就四舍五入。 例如,1.55会保留小数1.5的结果

静态int

ROUND_HALF_EVEN

roundingmodetoroundtowardsthe ' nearest neighbor ' unlessbothneighborsareequidistant,in which case,round towards the even neighbobobot

对于向最接近(距离)的一侧的舍入,如果是这样,则使用ROUND_HALF_UP作为保留位数,如果是偶数,则使用ROUND_HALF_DOWN作为保留位数

静态int

ROUND_HALF_UP

roundingmodetoroundtowards ' nearest neighbor ' unlessbothneighborsareequidistant,in which case round up。

除非两侧距离相等,否则向上舍入到最接近(距离)的一侧。 那样的话,就结束。 1.55保留小数1.6的结果

静态int

ROUND_UNNECESSARY

roundingmodetoassertthattherequestedoperationhasanexactresult,hence no rounding is necessary。

计算结果准确,不需要舍入模式

静态int

ROUND_UP

roundingmodetoroundawayfromzero。

向远离0的方向舍入

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。