Java-math-biginteger-abs

提供:Dev Guides
移動先:案内検索

Java.math.BigInteger.abs()メソッド

説明

  • java.math.BigInteger.abs()*は、値がこのBigIntegerの絶対値であるBigIntegerを返します。

宣言

以下は* java.math.BigInteger.abs()*メソッドの宣言です。

public BigInteger abs()

パラメーター

NA

戻り値

このメソッドは、呼び出された値の絶対値、つまりabs(this)を返します。

例外

NA

次の例は、math.BigInteger.abs()メソッドの使用方法を示しています。

package com.finddevguides;

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