【问题标题】:Regex Error in currency货币正则表达式错误
【发布时间】:2012-12-28 06:20:03
【问题描述】:

我有一个大问题,我用这个代码生成数字到货币,

Dim reg = New Regex("\d+")
Dim str As String = dZ.Rows(y).Item(14).ToString().Replace(",", ""). _
                               Replace(".00", "").Replace("$", "").Trim
Dim match = reg.Match(str)
If match.Success Then
    str = reg.Replace(str, Decimal.Parse(match.Value).ToString("C"))
End If

是的,它有效,但如果我的金额是:

1,900.50 POC

【问题讨论】:

  • 所以问题出在POC的多余字符上?除此之外,您还期待哪些字符串格式?
  • 我要做的就是将所有金额转换为货币

标签: regex vb.net currency


【解决方案1】:

kelvzy 你的解决方案不是很灵活。我会建议我的解决方案:

string cellValue = dZ.Rows[y][14];
string cleanedCellValue = Regex.Replace(s, @"\s[^\d.,]+", ""); 
//this will replace everything after the last digit

string decimalValue = Convert.ToDecimal(cleanedCellValue, CultureInfo.InvariantCulture);
string str = decimalValue.ToString("C");

当每个单元格使用逗号作为千位分隔符、点作为小数分隔符以及任何符号作为货币符号时,此解决方案将起作用。

在其他情况下,请给我更多的例子

【讨论】:

  • 这会转换成货币吗?样本“POC 19,000.50”
  • @kelvzy 从第二行删除 \s。它将工作string cleanedCellValue = Regex.Replace(s, @"[^\d.,]+", "");
猜你喜欢
  • 2012-11-30
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多