Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement