3

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
  • 17,159
  • 7
  • 50
  • 64
RanPaul
  • 3,484
  • 16
  • 53
  • 108

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
  • 46,408
  • 7
  • 75
  • 111
  • 1
    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