BigDecimal negate()

优质
小牛编辑
124浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.negate()返回一个BigDecimal,其值为(-this),其标度为this.scale()。

声明 (Declaration)

以下是java.math.BigDecimal.negate()方法的声明。

public BigDecimal negate()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回对象的负值,即-this。

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigDecimal.negate()方法的用法。

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create 2 BigDecimal objects
      BigDecimal bg1, bg2;
      // assign value to bg1
      bg1 = new BigDecimal("40.1264");
      // assign negate value of bg1 to bg2
      bg2 = bg1.negate();
      String str = "Negated value of " + bg1 + " is "+ bg2;
      // print bg1 and bg2 values
      System.out.println( str );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Negated value of 40.1264 is -40.1264