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

前端 未结 10 565
旧巷少年郎
旧巷少年郎 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:13

    I know its too late. However following worked for me :

    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.UK);
    new DecimalFormat("\u00A4#######0.00",otherSymbols).format(totalSale);
    
     \u00A4 : acts as a placeholder for currency symbol
     #######0.00 : acts as a placeholder pattern for actual number with 2 decimal 
     places precision.   
    

    Hope this helps whoever reads this in future :)

提交回复
热议问题