Search code examples
javacurrencynumber-formatting

Format negative amount of USD with a minus sign, not brackets (Java)


How do I get NumberFormat.getCurrencyInstance() to print negative USD currency values with a minus sign?


Solution

  • Since I faced this problem again, I did some research and found a more resilient solution provided by the ICU:

    NumberFormatter
      .withLocale(...)
      .unit(Currency.getInstance("USD"))
      .sign(SignDisplay.AUTO) // "123", "0", and "-123"
      .format(123)
      .toString();
    

    Check the API documentation of NumberFormatter for more details.