如何使用特定格式显示数字(Android Eclipse,Java)

时间:2013-07-25 16:30:59

标签: java android eclipse

double PV = 154055.054847215

如何将其显示为1,540,055.055?

3 个答案:

答案 0 :(得分:4)

将此DecimalFormat实例添加到方法的顶部:

DecimalFormat three = new DecimalFormat("#,##0.000"); // will display numbers separated by commas every three digits to the left of the decimal, and will round it to three decimal places. No more, no less.

// the three zeros after the decimal point above specify how many decimal places to be accurate to.
// the zero to the left of the decimal place above makes it so that numbers that start with "0." will display "0." vs just "." If you don't want the "0.", replace that 0 to the left of the decimal point with "#"

然后,调用实例“3”并在显示时传递double值:

double PV = 154055.054847215;
display.setText(three.format(PV)); // displays 1,540,055.055

答案 1 :(得分:1)

试试这个:

DecimalFormat formatter = new DecimalFormat("#,###.00");

System.out.println(formatter.format(PV));

Source

答案 2 :(得分:1)

String s = String.format(%,.2f, PV);

System.out.println(s);