java.math.BigInteger.abs()返回一個BigInteger,其值是此BigInteger的絕對值。
以下是java.math.BigInteger.abs()方法的宣告
public BigInteger abs()
NA
此方法返回撥用值,即絕對值 abs(this).
NA
下面的例子顯示math.BigInteger.abs()方法的用法
package com.yiibai; import java.math.*; public class BigIntegerDemo { public static void main(String[] args) { // create 4 BigInteger objects BigInteger bi1, bi2, bi3, bi4; // assign values to bi1, bi2 bi1 = new BigInteger("123"); bi2 = new BigInteger("-123"); // assign absolute values of bi1, bi2 to bi3, bi4 bi3 = bi1.abs(); bi4 = bi2.abs(); String str1 = "Absolute value of " + bi1 + " is " + bi3; String str2 = "Absolute value of " + bi2 + " is " + bi4; // print bi3, bi4 values System.out.println( str1 ); System.out.println( str2 ); } }
讓我們編譯和執行上面的程式,這將產生以下結果:
Absolute value of 123 is 123 Absolute value of -123 is 123