- Java.math 软件包附加内容
- Java.math - 枚举
- Java.math - 讨论
Java.math.BigDecimal.negate() 方法
说明
java.math.BigDecimal.negate(MathContext mc) 返回一个 BigDecimal,其值根据上下文的设置进行舍入,即返回 (-this)。
声明
以下是 java.math.BigDecimal.negate() 方法的声明。
public BigDecimal negate(MathContext mc)
参数
mc - 要使用的上下文。
返回值
此方法返回对象(即 - this)的否定值,根据需要进行舍入。
异常
ArithmeticException - 如果结果不精确但舍入模式为 UNNECESSARY。
示例
以下示例演示了使用 math.BigDecimal.negate() 方法。
package com.tutorialspoint;
import java.math.*;
public class BigDecimalDemo {
public static void main(String[] args) {
// create 2 BigDecimal objects
BigDecimal bg1, bg2;
MathContext mc = new MathContext(4);// 4 precision
// assign value to bg1
bg1 = new BigDecimal("40.1264");
// assign negate value of bg1 to bg2 using mc
bg2 = bg1.negate(mc);
String str = "Negated value, rounded to 2 precision is " +bg2;
// print bg2 value
System.out.println( str );
}
}
让我们编译并运行以上程序,这将产生以下结果 -
Negated value, rounded to 2 precision is -40.13
java_math_bigdecimal.htm
广告