Display Negative Number in Parentheses in Java

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.

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

Positive Number: 250.00
Negative Number: (250.00)

If you don’t need decimal places, you can change to pattern to:

 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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.