目录

String toString()

描述 (Description)

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

返回的String表示MathContext对象的设置为两个以空格分隔的单词(由单个空格字符'\ u0020'分隔,并且没有前导或尾随空格),如下所示 -

  • 字符串“precision =”,后面紧跟精度设置的值作为数字字符串,就像由Integer.toString方法生成一样。

  • 字符串“roundingMode =”,紧接着是roundingMode设置的值作为单词。 该单词与RoundingMode枚举中相应公共常量的名称相同。

如果将更多属性添加到此类,将来可能会将附加单词附加到toString的结果中。

声明 (Declaration)

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

public String toString()

覆盖 (Overrides)

Object toString。

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回表示上下文设置的String。

异常 (Exception)

NA

例子 (Example)

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

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018