簡體   English   中英

如何使用Groovy從字符串1,398.90中刪除逗號

[英]How to remove comma from string 1,398.90 using groovy

我無法使用groovy從字符串1,398.90中刪除逗號

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

我真的會避免使用帶有數字的正則表達式

特別是看起來像錢的數字numbers

您可以使用DecimalFormat將String讀入BigDecimal(這樣就可以保持精度)

import java.text.*

BigDecimal result = DecimalFormat.instance.with {
    parseBigDecimal = true
    parse('1,398.90')
}

如@daggett所述,您的代碼可以正常工作。 除了正則表達式或替換以外的另一種替代方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM