使用此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); //除法 val1 = val2.negate(); System.out.println("Result (Negation) = "+val1); } }
输出结果
Value 1 : 37578975587 Value 2 : 62567875598 Result (Negation) = -62567875598
让我们看另一个例子-
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