我想转换我的最终双倍价值 即(1.0) 打印出来的价格只要$ 1.00。这在Java中怎么可能?
我导入了java.lang。* DecimalFormat formatter = new DecimalFormat(“#。00”);
不确定如何在那里获得美元符号。除了去“$”+ .....
之外,我不知道是否还有其他办法可以做到这一点答案 0 :(得分:0)
// Format double with 2 decimal places and add a dollar sign
String price = "$" + String.format("%.2f", myDouble);
要直接打印,请使用PrintWriter.printf
,例如在System.out
:
System.out.printf("$%.2f", myDouble);
答案 1 :(得分:0)
使用DecimalFormat
double value = 1.0;
DecimalFormat dformat = new DecimalFormat("#.00");
String modifiedVal = dformat.format(value);
System.out.println("$"+modifiedVal);
答案 2 :(得分:0)
NumberFormat类有一个预定义的货币格式化实例。
double value = 1.0;
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(value));
答案 3 :(得分:0)
您可以使用Locale
& Java中的NumberFormat
个类。
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en","US"));
System.out.println(formatter.format(1.0));