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.