在 Java 中对 BigDecimal 值求反


使用 negate() 方法在 Java 中对 BigDecimal 值求反。该方法返回一个 BigDecimal,其值为 (-this),并且其刻度为 this.scale()。

以下是一个示例 -

示例

 在线演示

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal val1 = new BigDecimal("37578975587");
      BigDecimal val2 = new BigDecimal("62567875598");
      System.out.println("Value 1 : "+val1);
      System.out.println("Value 2 : "+val2);
      // division
      val1 = val2.negate();
      System.out.println("Result (Negation) = "+val1);
   }
}

输出

Value 1 : 37578975587
Value 2 : 62567875598
Result (Negation) = -62567875598

让我们看另一个示例 -

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

示例

 在线演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigDecimal bg1, bg2;
      bg1 = new BigDecimal("68.99898");
      bg2 = bg1.negate();
      String str = "Negated value of " + bg1 + " is "+ bg2;
      System.out.println( str );
   }
}

输出

Negated value of 68.99898 is -68.99898

更新时间:2019 年 7 月 30 日

248 次查看

开启你的职业生涯

通过完成课程获得认证

开始
广告