从NumberFormat格式中删除货币字符()

时间:2014-04-10 20:22:55

标签: java groovy formatting

在使用NumberFormat格式化货币后尝试删除货币字符

import java.text.NumberFormat;

BigDecimal currencyAmount = new BigDecimal(9876543.21)
def currentLocale = Locale.US
def currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);

println "formatted currency = "+currencyFormatter.format(currencyAmount)

这会打印$9,876,543.21,但我不想要格式化货币中的$或任何货币字符。反正有吗?

1 个答案:

答案 0 :(得分:6)

Groovy解决方案:

import java.text.NumberFormat

def currencyAmount = 9876543.21 //Default is BigDecimal
def currencyFormatter = NumberFormat.getInstance( Locale.US )

assert currencyFormatter.format( currencyAmount ) == "9,876,543.21"

如果不需要货币,则不需要getCurrencyInstance()