문제

What is the use of the negative pattern?

NumberFormat nf = NumberFormat.getPercentInstance(Locale.ITALY);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("###,###.###;(###,###.###)");
System.err.println(df.format(-12.45));//output (12,45);

In what scenario would it be useful? Isn't just a module?

도움이 되었습니까?

해결책

The negative pattern is simply a different way to print out negative numbers. You may want a wide variety of choices:

 1,234       1,234         1,234        1,234 CR
-1,234       1,234-       (1,234)       1,234 DR

and so on. Each of these is useful in their different domains. The (1,234) variant is actually quite useful if you don't have a colour printer since it stands out a lot more than just a leading negative and it's used quite a bit in bookkeeping and accounting areas.

My own favorite (in a non-Java setting, Libre Office) is:

"#,##9.99 ;[RED](#,##9.99-)"

which outputs the negative numbers in red with a trailing - sign and surrounded by parentheses, so there's no doubt it's a negative value:

enter image description here

다른 팁

Believe it or not that I've seen multiple business applications that format their negative values in exactly this manner. So I'm afraid the use is because people need/use it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top