當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.MathContext.toString()用法及代碼示例


描述

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

返回的字符串將 MathContext 對象的設置表示為兩個 space-delimited 字(由單個空格字符 '\u0020' 分隔,並且沒有前導或尾隨空格),如下所示

  • 字符串“precision = ”,緊接著精度設置的值作為數字字符串,就像由 Integer.toString 方法生成的一樣。

  • 字符串“roundingMode =”,緊接著是roundingMode 設置的值作為一個字。這個詞將與 RoundingMode 枚舉中相應公共常量的名稱相同。

如果向此類添加更多屬性,將來可能會在 toString 的結果中附加額外的單詞。

聲明

以下是聲明java.math.MathContext.toString()方法。

public String toString()

覆蓋

類中的 toStringObject

參數

NA

返回值

此方法返回一個表示上下文設置的字符串。

異常

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.toString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。