0

I am not able to remove comma from string 1,398.90 using groovy

def liveprice = '1,398.90'; def liveprice2 = liveprice.replaceAll(',', '')

Guru
  • 11
  • 2

2 Answers2

1

I would really avoid using regular expressions with numbers

Especially numbers that look like money

You can use DecimalFormat to read that String into a BigDecimal (so you keep precision)

import java.text.*

BigDecimal result = DecimalFormat.instance.with {
    parseBigDecimal = true
    parse('1,398.90')
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

As mentioned by @daggett, your code works fine. Another alternative way besides regex or replace:

'1,39,9,,,,.90'.split(",").join()
// outputs: 1399.90