Question

Am trying to remove the currency character after formatting the currency using 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)

This prints $9,876,543.21 but I dont want $ or any currency character in the formatted currency. Is there anyway to do it?

Was it helpful?

Solution

Groovy Solution:

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"

Don't need getCurrencyInstance() if currency is not requried.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top