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
  • 19,094
  • 7
  • 58
  • 72
OTUser
  • 3,788
  • 19
  • 69
  • 127

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
  • 49,365
  • 7
  • 88
  • 117
  • 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