本文介紹了強制點(“.")作為java中的小數分隔符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我目前使用以下代碼打印雙精度:
I currently use the following code to print a double:
return String.format("%.2f", someDouble);
這很好用,除了 Java 使用我的語言環境的小數分隔符(逗號)而我想使用點.有沒有簡單的方法可以做到這一點?
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
可讓您指定語言環境:
Use the overload of String.format
which lets you specify the locale:
return String.format(Locale.ROOT, "%.2f", someDouble);
如果您僅 格式化數字 - 就像您在這里一樣 - 那么使用 NumberFormat
可能更合適.但是,如果您需要 String.format
的其余格式化功能,這應該可以正常工作.
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.
這篇關于強制點(“.")作為java中的小數分隔符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!