Java.math.MathContext.toString() 方法



描述

java.math.MathContext.toString() 返回此 MathContext 的字串表示形式。

返回的 String 表示 MathContext 物件的設定,作為兩個空格分隔的單詞(由單個空格字元“\u0020”分隔,且沒有前導或尾隨空格),如下所示 −

  • 字串“precision =”,後面緊跟精度設定的值,該值是數字字串,如同由 Integer.toString 方法生成一樣。

  • 字串“roundingMode =”,後面緊跟舍入模式設定的值,該值是一個單詞。此單詞與 RoundingMode 列舉中相應公有常量的名稱相同。

如果為此類新增更多屬性,則將來可能會將其他單詞附加到 toString 的結果中。

宣告

以下是 java.math.MathContext.toString() 方法的宣告。

public String toString()

重寫

Object 類中的 toString。

引數

不適用於 (NA)

返回值

此方法返回表示上下文設定的 String。

異常

不適用於 (NA)

示例

以下示例展示了 math.MathContext.toString() 方法的用法。

package com.tutorialspoint;

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
java_math_mathcontext.htm
廣告
© . All rights reserved.