4

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?

Madbreaks
  • 18,119
  • 7
  • 53
  • 69
OTUser
  • 3,656
  • 18
  • 59
  • 119

1 Answers1

6

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.

dmahapatro
  • 48,012
  • 7
  • 84
  • 116
  • 2
    This doesn't work with whole numbers: `5280` will be `5,280` and not `5,280.00` which is what op was wanting. – Madbreaks Mar 01 '18 at 01:06