本文介紹了格式化帶前導符號的數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何在 Java 中格式化帶有前導符號的數字?
How do I format in Java a number with its leading sign?
前導-
可以正確顯示負數,但是+
明顯不顯示正數.
Negative numbers are correctly displayed with leading -
, but obviously positive numbers are not displayed with +
.
如何在 Java 中做到這一點?我當前的貨幣格式字符串是 ###,###,###,###,##0.00
(是的,我需要格式化正/負貨幣值)
How to do that in Java? My current currency format string is ###,###,###,###,##0.00
(yes, I need to format positive/negative currency values)
推薦答案
使用否定子模式,如 DecimalFormat 的 javadoc.
Use a negative subpattern, as described in the javadoc for DecimalFormat.
DecimalFormat fmt = new DecimalFormat("+#,##0.00;-#");
System.out.println(fmt.format(98787654.897));
System.out.println(fmt.format(-98787654.897));
產生(在我的法語語言環境中,空格是分組分隔符,逗號是小數分隔符):
produces (in my French locale where space is the grouping separator and the comma is the decimal separator) :
+98?787?654,90
-98?787?654,90
這篇關于格式化帶前導符號的數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!