Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the negative pattern?

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?

like image 409
Rollerball Avatar asked Dec 21 '22 00:12

Rollerball


2 Answers

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

like image 87
paxdiablo Avatar answered Jan 08 '23 08:01

paxdiablo


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.

like image 28
Gareth Davis Avatar answered Jan 08 '23 08:01

Gareth Davis