問(wèn)題描述
如何在 Java 中格式化帶有前導(dǎo)符號(hào)的數(shù)字?
How do I format in Java a number with its leading sign?
前導(dǎo)-
可以正確顯示負(fù)數(shù),但是+
明顯不顯示正數(shù).
Negative numbers are correctly displayed with leading -
, but obviously positive numbers are not displayed with +
.
如何在 Java 中做到這一點(diǎn)?我當(dāng)前的貨幣格式字符串是 ###,###,###,###,##0.00
(是的,我需要格式化正/負(fù)貨幣值)
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));
產(chǎn)生(在我的法語(yǔ)語(yǔ)言環(huán)境中,空格是分組分隔符,逗號(hào)是小數(shù)分隔符):
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
這篇關(guān)于格式化帶前導(dǎo)符號(hào)的數(shù)字的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!