java中toPlainString()和toString()之间的区别是什么?
请告诉我这两种方法的区别。提前感谢。Java toString()方法: 如果要将任何对象表示为字符串,请使用toString()方法 toString()方法返回字符串 对象的表示 例如:java中toPlainString()和toString()之间的区别是什么?,java,string,bigdecimal,Java,String,Bigdecimal,请告诉我这两种方法的区别。提前感谢。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.to
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
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
Java toPlainString()方法:
toPlainString()返回一个字符串
不带指数字段的此BigDecimal的表示形式
例如:
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
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
您是否尝试过查看Javadocs?值得一提的是,每个对象都有toString()方法,因为它是在对象类本身中定义的!双numDb=1.00000E-28;BigDecimal b=新的BigDecimal(numDb);字符串s=b.toString();系统输出打印项次;上面的代码片段对我来说很有用。这就是为什么我混淆了这两种方法之间的区别。