![](/img/trans.png)
[英]java: How do I use printf to print a dollar sign and a double with 2 decimals?
[英]How do I put a dollar sign when using printf in java?
我想在可變price
前面放一個美元符號並保持相同的格式,但如果我只是把它放在%36
前面,它將相距大約 36 個字符。
System.out.printf(" Sale Price %36.2f", price);
您可以執行以下操作:
NumberFormat nf=NumberFormat.getCurrencyInstance();
System.out.printf(" Sale Price %36s", nf.format(price));
基本上使用貨幣數字格式並將其格式化為您想要的字符串。 然后你使用 %36 在你想要的 position 打印結果字符串。
結果:
Sale Price $12.30
我希望這對你有用
DecimalFormat ft = new DecimalFormat("####");
double price = 23456.789;
ft = new DecimalFormat("$###,###.##");
System.out.println("Sale Price: " + ft.format(price));
答案是
Sale Price: $23,456.79
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.