In this post, we will see how to display negative number in Parentheses in Java.
We can use pattern #,##0.00;(#,##0.00)
with DecimalFormat‘s format()
to display negative number in Parenthesis in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.text.DecimalFormat; public class DisplayNegativeNumbersInParenthesis { public static void main(String[] args) { DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(); String pattern = "#,##0.00;(#,##0.00)"; df.applyPattern(pattern); String positiveNo= df.format(250); String NegativeNo= df.format(-250); System.out.println("Positive Number: " + positiveNo); System.out.println("Negative Number: " + NegativeNo); } } |
Output
1 2 3 4 |
Positive Number: 250.00 Negative Number: (250.00) |
If you don’t need decimal places, you can change to pattern to:
1 2 3 |
String pattern = "#,##0;(#,##0)"; |
Let’s understand meaning of pattern #,##0.00;(#,##0.00)
:
- First part of up to semicolon is used to format positive numbers
- Second part after semicolon is used to format negative numbers with parenthesis.
That’s all about how to display negative number in Parentheses in Java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.