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

前端 未结 7 1499
离开以前
离开以前 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:16

    Here is one I always end up using either in a java class or via the fmt:formatNumber jstl tag:

    DecimalFormat format = new DecimalFormat("$#,##0.00;$-#,##0.00");
    String formatted = format.format(15.5);
    

    It always produces at least a $0.00 and is consistent when displayed. Also includes thousands seperators where needed. You can move the minus sign in front of the dollar sign if that is your requirement.

提交回复
热议问题