Java BigDecimal negate() 方法

2025 年 3 月 20 日 | 阅读 2 分钟

java BigDecimal 类的 negate() 方法用于获取一个 BigDecimal 对象,其值为使用该方法的 BigDecimal 对象的相反数(-this)。

语法

参数

mc: 要使用的上下文。

Exception

ArithmeticException - 如果结果不精确,但当前的舍入模式是 UNNECESSARY,则会抛出此异常。

返回值

它返回 -this(取反后的 BigDecimal 值),并根据需要进行舍入。

示例 1

输出

Negated value of 5656 is =  -5656

示例 2

输出

Negated value of  -152207  is =  152207

示例 3

输出

Negated value of  -18.5414 rounded by 4  is =  18.54

示例 4

输出

Negated value of  -178901456.68431 rounded by 16  is =  178901456.68431
 
下一主题Java BigDecimal