Regex Error in currency

和自甴很熟 提交于 2019-12-13 06:07:59

问题


I have a big problem, I used this codes to generate the number to currency,

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

yes it works, but what if my amount is:

1,900.50 POC

回答1:


kelvzy your solution is not very flexible. I will suggest my solution:

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");

This solution will work, when each cell uses comma as thousand separator, dot as decimal separator and any symbols as currency symbol.

In other case give me please more examples



来源:https://stackoverflow.com/questions/14065466/regex-error-in-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!