問題描述
我目前使用以下代碼打印雙精度:
I currently use the following code to print a double:
return String.format("%.2f", someDouble);
這很好用,除了 Java 使用我的語言環(huán)境的小數(shù)分隔符(逗號(hào))而我想使用點(diǎn).有沒有簡(jiǎn)單的方法可以做到這一點(diǎn)?
This works well, except that Java uses my Locale's decimal separator (a comma) while I would like to use a point. Is there an easy way to do this?
推薦答案
使用String.format
可讓您指定語言環(huán)境:
Use the overload of String.format
which lets you specify the locale:
return String.format(Locale.ROOT, "%.2f", someDouble);
如果您僅 格式化數(shù)字 - 就像您在這里一樣 - 那么使用 NumberFormat
可能更合適.但是,如果您需要 String.format
的其余格式化功能,這應(yīng)該可以正常工作.
If you're only formatting a number - as you are here - then using NumberFormat
would probably be more appropriate. But if you need the rest of the formatting capabilities of String.format
, this should work fine.
這篇關(guān)于強(qiáng)制點(diǎn)(“.")作為java中的小數(shù)分隔符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!