Java toString()方法:
如果要将任何对象表示为字符串,则toString()方法就会存在.toString()方法返回对象的字符串表示形式.
示例:
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
Run Code Online (Sandbox Code Playgroud)
Java toPlainString()方法:
java.math.BigDecimal.toPlainString()返回此BigDecimal的字符串表示形式,不带指数字段.
示例:
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
Run Code Online (Sandbox Code Playgroud)