Java-math-mathcontext-tostring

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

Java.math.MathContext.toString()メソッド

説明

  • java.math.MathContext.toString()*は、このMathContextの文字列表現を返します。

返される文字列は、次のように、MathContextオブジェクトの設定を、スペースで区切られた2つの単語(単一のスペース文字 '\ u0020’で区切られ、先頭または末尾の空白なし)として表します-

  • 文字列 "precision ="。直後にInteger.toStringメソッドによって生成されたかのように、数値文字列としての精度設定の値が続きます。
  • 文字列 "roundingMode ="の直後に単語としてのroundingMode設定の値が続きます。 この単語は、RoundingMode列挙型の対応するパブリック定数の名前と同じです。

このクラスにさらにプロパティが追加される場合、将来的にtoStringの結果に追加の単語が追加される可能性があります。

宣言

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

public String toString()

オーバーライド

クラス Object のtoString。

パラメーター

NA

戻り値

このメソッドは、コンテキスト設定を表す文字列を返します。

例外

NA

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

package com.finddevguides;

import java.math.*;

public class MathContextDemo {

   public static void main(String[] args) {

     //create 2 MathContext objects
      MathContext mc1, mc2;

     //assign context settings to mc1, mc2
      mc1 = new MathContext(6, RoundingMode.DOWN);
      mc2 = new MathContext(20, RoundingMode.FLOOR);

     //create 2 String objects
      String s1, s2;

     //assign string representation of mc1, mc2 to s1, s2
      s1 = mc1.toString();
      s2 = mc2.toString();

      String str1 = "String representation of mc1 is " + s1;
      String str2 = "String representation of mc2 is " + s2;

     //print s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

String representation of mc1 is precision = 6 roundingMode = DOWN
String representation of mc2 is precision = 20 roundingMode = FLOOR