我有以下代码,
Scanner cin = new Scanner(System.in);
System.out.print("Part-1- Enter the amount inout ($ 1,567.00):");
String amountStr = cin.nextLine();
String amountStr2 = amountStr.replace("$" , "");
String amountStr3 = amountStr2.replace("," , "");
double amountDouble = Double.parseDouble(amountStr3);
double quarters = (amountDouble / .25);
double quarters2 = Math.floor(quarters);
int quartersFinal = (int) quarters2;
我想结果返回数字结果6,269。现在,代码返回6269.我如何添加逗号?
答案 0 :(得分:2)
您可以使用DecimalFormat
Scanner cin = new Scanner(System.in);
System.out.print("Part-1- Enter the amount inout ($ 1,567.00):");
String amountStr = cin.nextLine();
String amountStr2 = amountStr.replace("$" , "");
String amountStr3 = amountStr2.replace("," , "");
double amountDouble = Double.parseDouble(amountStr3);
double quarters = (amountDouble / .25);
double quarters2 = Math.floor(quarters);
DecimalFormat df = new DecimalFormat("#,###");
System.out.println(df.format(quarters2));
Out put:
6,268
答案 1 :(得分:0)
试试这个
DecimalFormat df = new DecimalFormat("#,###.##");
String amountStr2 = amountStr.replace("$" , "");
String amountStr3 = amountStr2.replace("," , "");
double amountDouble = Double.parseDouble(amountStr3);
df.format(amountDouble);
答案 2 :(得分:0)
详细阅读string.Format(...)的文档。你将来需要多次。