java java中的 toPlainString() 和 toString() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33563946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:51:13  来源:igfitidea点击:

What is the difference between toPlainString() and toString() in java?

javastringbigdecimal

提问by Siva Sankar

Kindly let me know the difference between this two methods. Thanks in advance.

请让我知道这两种方法之间的区别。提前致谢。

回答by Shiladittya Chakraborty

Java toString() method :

Java toString() 方法:

If you want to represent any object as a string, toString() method comes into existence.The toString() method returns the string representation of the object.

如果要将任何对象表示为字符串,toString() 方法就应运而生。toString() 方法返回对象的字符串表示形式。

Example :

例子 :

Student s1 = new Student(101,"Raj","lucknow");  
Student s2 = new Student(102,"Vijay","ghaziabad");  

System.out.println(s1);//compiler writes here s1.toString()  
System.out.println(s2);//compiler writes here s2.toString()  

//Output : 101 Raj lucknow
           102 Vijay ghaziabad

Java toPlainString() method :

Java toPlainString() 方法:

The java.math.BigDecimal.toPlainString() returns a string representation of this BigDecimal without an exponent field.

java.math.BigDecimal.toPlainString() 返回此 BigDecimal 的字符串表示形式,没有指数字段。

Example :

例子 :

MathContext mc = new MathContext(3); // 3 precision
BigDecimal bigDecimal = new BigDecimal("1234E+4", mc);
// Assign the plain string value of bigDecimal to s
String plainString = bigDecimal.toPlainString();

String str = "Plain string value of " + bigDecimal + " is " + plainString;

// print s value
System.out.println( str );

//Output : Plain string value of 1.23E+7 is 12300000