在使用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
,但我不想要格式化货币中的$或任何货币字符。反正有吗?
答案 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()
。