How to use Java's DecimalFormat for “smart” currency formatting?

前端 未结 10 567
旧巷少年郎
旧巷少年郎 2021-01-01 09:34

I\'d like to use Java\'s DecimalFormat to format doubles like so:

#1 - 100 -> $100
#2 - 100.5 -> $100.50
#3 - 100.41 -> $100.41

Th

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 10:31

    printf also works.

    Example:

    double anyNumber = 100; printf("The value is %4.2f ", anyNumber);

    Output:

    The value is 100.00

    4.2 means force the number to have two digits after the decimal. The 4 controls how many digits to the right of the decimal.

提交回复
热议问题