字符串双精度值-
toString (类双精度方法
此方法返回当前双精度对象的字符串格式。 将双精度值转换为字符串。 将所需的原始double值读取到double类引用变量中(发生自动包装)。
toString (使用方法转换为String。
注意可以将双精度值直接传递给toString (方法-
importjava.util.Scanner示例;
publicclassConversionOfDouble{
publicstaticvoidmain (string args [ ] ) {
sannersc=newscanner(system.in );
System.out.println ('输入双精度值:');
双精度=sc.next双精度(;
Stringresult=d.toString (;
system.out.println (' theresultis : ' result );
系统. out.println (double.tostring (d ) );
}
}
要输出结果,请输入双精度值。
2548.2325
Theresultis:2548.2325
2548.2325
valueOf ) )字符串类的方法
此方法将char或char数组、double或float或int或long对象作为参数,并返回其字符串表示形式。 要将双精度值转换为String-,请获取-双精度值。
将其作为参数传递给此方法以获取字符串格式。
importjava.util.Scanner示例;
publicclassConversionOfDouble{
publicstaticvoidmain (string args [ ] ) {
sannersc=newscanner(system.in );
System.out.println ('输入双精度值:');
双精度=sc.next双精度(;
字符串结果=' '.value of (d;
system.out.println (' theresultis : ' result );
}
}
要输出结果,请输入双精度值。
2548.2325
Theresultis:2548.2325
format () String类方法
此方法接受字符串和参数(变量)的格式,并返回以指定格式指定的变量的String对象。 要将双精度值转换为String-,请获取-双精度值。
通过将“%f”作为格式字符串传递并传递双精度值来调用format ()方法。
importjava.util.Scanner示例;
publicclassConversionOfDouble{
publicstaticvoidmain (string args [ ] ) {
sannersc=newscanner(system.in );
System.out.println ('输入双精度值:');
双精度=sc.next双精度(;
stringresult=string.format('%f ',d );
system.out.println (' theresultis : ' result );
}
}
要输出结果,请输入双精度值。
2548.2325
Theresultis:2548.2325
应用(stringbuilder或StringBuffer方法
StringBuilder或StringBuffer对象的append ()方法接受布尔值、char或char数组、双精度或浮点、整型或长整型或字符串值作为参数取得双精度的值。
使用append ()方法附着到StringBuffer对象。
toString (使用方法获取StringBuffer对象的字符串值。
importjava.util.Scanner示例;
publicclassConversionOfDouble{
publicstaticvoidmain (string args [ ] ) {
sannersc=newscanner(system.in );
System.out.println ('输入双精度值:');
双精度=sc.next双精度(;
StringBuffersb=newStringBuffer (;
sb.append(d;
Stringresult=sb.toString (;
system.out.println (' theresultis : ' result );
}
}
要输出结果,请输入双精度值。
2548.2325
Theresultis:2548.2325