使用正则表达式将点(.)替换为逗号(,)?

发布于 2024-09-13 04:04:33 字数 252 浏览 1 评论 0原文

我正在开发一个 C# 应用程序。我想用逗号(,)更改数字十进制数字,其中我使用正则表达式有点(。)。

例如:

Price= 100,00.56

作为表示数值的国际规则,但在瑞典,他们对数字有不同的方式,比如

Price= 100.00,56

所以我想使用正则表达式将点(.)更改为逗号(,)并将逗号(,)更改为点(.)。可以指导我这件事。

I am working on a C# application. I want to change number decimal figure with comma(,) where i have dot(.) using regular expression.

For example:

Price= 100,00.56

As this international rule of representing numeric values but I Sweden they have different ways for numbers Like

Price= 100.00,56

So i want to change dot(.) into comma(,) and comma(,) into dot(.) using RegEx. Could guide me about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

魂牵梦绕锁你心扉 2024-09-20 04:04:33

设置数字格式时,应使用字符串格式重载,该重载采用 < a href="http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx" rel="noreferrer">CultureInfo 对象。瑞典语的区域性名称是“sv-SE”,如此处所示

decimal value = -16325.62m;
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("sv-SE")));

编辑

正如 @OregonGhost 指出的那样 - 解析数字也应该使用 CultureInfo 来完成。

When formatting numbers, you should use the string format overload that takes a CultureInfo object. The culture name for swedish is "sv-SE", as can be seen here.

decimal value = -16325.62m;
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("sv-SE")));

Edit:

As @OregonGhost points out - parsing out numbers should also be done with CultureInfo.

空袭的梦i 2024-09-20 04:04:33

不是正则表达式解决方案,但根据我的经验 - 更正确:

public static string CheckDecimalDigitsDelimiter(this string instance)
{
    var sv = new CultureInfo("sv-SE");
    var en = new CultureInfo("en-US");

    decimal d;
    return (!Decimal.TryParse(instance, NumberStyles.Currency, sv, out d) &&
            Decimal.TryParse(instance, NumberStyles.Currency, en, out d)) ?
        d.ToString(sv) : // didn't passed by SV but did by EN
        instance;
}

这个方法有什么作用?它确保如果给定的字符串是不正确的瑞典字符串,但英语是正确的 - 将其转换为瑞典,例如 100,00 -> 100,00100.00 -> 100,00

Not a RegEx solution but from my experience - more correct:

public static string CheckDecimalDigitsDelimiter(this string instance)
{
    var sv = new CultureInfo("sv-SE");
    var en = new CultureInfo("en-US");

    decimal d;
    return (!Decimal.TryParse(instance, NumberStyles.Currency, sv, out d) &&
            Decimal.TryParse(instance, NumberStyles.Currency, en, out d)) ?
        d.ToString(sv) : // didn't passed by SV but did by EN
        instance;
}

What does this method do? It ensures that if given string is incorrect Sweden string but is correct English - convert it to Sweden, e.g. 100,00 -> 100,00 but 100.00 -> 100,00.

忘羡 2024-09-20 04:04:33

即使没有正则表达式,您也可以做到这一点。例如

var temp = price.Replace(".", "<TEMP>");
var temp2 = temp.Replace(",", ".");
var replaced = temp2.Replace("<TEMP>", ",");

You can do this even without regex. For example

var temp = price.Replace(".", "<TEMP>");
var temp2 = temp.Replace(",", ".");
var replaced = temp2.Replace("<TEMP>", ",");
逆夏时光 2024-09-20 04:04:33

还可以看看

System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator

Also have a look at

System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
云醉月微眠 2024-09-20 04:04:33

不确定 100,00.56 代表什么,您的意思是 10.000,56 吗?

回答你的问题:

对于如此简单的任务,为什么使用正则表达式?您可以更轻松地做到这一点:

string oldValue = "100,00.56";
char dummyChar = '&'; //here put a char that you know won't appear in the strings
var newValue = oldValue.Replace('.', dummyChar)
                       .Replace(',', '.')
                       .Replace(dummyChar, ',');

编辑
我同意@Oded,对于格式化数字,请使用 CultureInfo 类。

Not sure what 100,00.56 represents, did you mean 10.000,56?

To answer your question:

For such a simple task, why use RegEx? You can do it much easier:

string oldValue = "100,00.56";
char dummyChar = '&'; //here put a char that you know won't appear in the strings
var newValue = oldValue.Replace('.', dummyChar)
                       .Replace(',', '.')
                       .Replace(dummyChar, ',');

Edit
I agree with @Oded, for formatting numbers use the CultureInfo class.

明媚如初 2024-09-20 04:04:33

不要依赖 RegExp 来处理这种事情:) 使用文化 fx 中的构建:

decimal s = decimal.Parse("10,000.56", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"));
string output = s.ToString("N",CultureInfo.GetCultureInfo("da-DK"));

en-US 将正确解析它,而 da-DK 使用另一种表示形式。我住在 DK,因此使用它,但你应该使用适合你输出的文化。

Do not rely on RegExp for this kind of thing :) Use the build in cultures fx:

decimal s = decimal.Parse("10,000.56", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"));
string output = s.ToString("N",CultureInfo.GetCultureInfo("da-DK"));

en-US will parse it correctly and da-DK uses the other kind of representation. I live in DK and therefore use that but you should use the culture which fits your output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文