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

前端 未结 7 1488
离开以前
离开以前 2020-12-03 09:49

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

7条回答
  •  青春惊慌失措
    2020-12-03 10:19

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

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

    Check API documentation of NumberFormatter for more details.

提交回复
热议问题