哪个数字<->无论本地化设置如何,字符串转换始终保持一致?

发布于 2024-09-12 16:53:51 字数 1362 浏览 5 评论 0 原文

我对数字有一些问题<->字符串转换我无法解决。

我们编写了一个使用多种 xml 序列化技术的序列化框架。但我在生成的输出中看到了一些不一致的数字转换:

使用:

var c = TypeDescriptor.GetConverter(double);
var s = c.ConvertToString(value);

并且

var c = TypeDescriptor.GetConverter(double);
return c.ConvertFromString(value.Value);

我得到带有逗号分隔符的字符串,例如 3,33 并转换回双精度,但是转换回双精度在另一个使用点作为分隔符的国家/地区也可以工作吗? (我在德国,我们使用逗号,但除此之外,我想要点分隔符)

然后在代码的另一部分中,我通过将某些结构中的双精度数添加到 XElement 来直接转换它:

data.Add(new XElement("x", vector.x));

XElement 构造函数将双精度数转换为带点的字符串,如“3.33”。

当我想将这个字符串转换回双精度数时,这真的很奇怪:

Double.TryParse(item.Value, out value.x);

并且

value.x = Convert.ToDouble(item.Value);

都忽略了点并且我有一个双精度值 333!?

所以我也用 TypeConverter 代码尝试了它,但它给了我一个例外,即“3.33”对于双精度转换器来说不是有效的字符串......

现在我真正需要的是一个数字 <->字符串转换始终使用点分隔符并正确解析它,无论文化/本地化设置如何...

我迷路了..我应该使用哪个?我什至尝试过类似的事情:

    NumberFormatInfo provider = new NumberFormatInfo();

    provider.NumberDecimalSeparator = ".";
    provider.NumberGroupSeparator = " ";
    provider.NumberGroupSizes = new int[] { 3 };

    foreach (var item in CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
        item.NumberFormat = provider;
    }

但这没有帮助.. 感谢您的任何提示..

I have some issues with number <-> string conversion i cannot sort out.

We have written a serialization framework which uses several xml serialization techniques. But i have seen some inconsistent number conversions in the generated output:

using:

var c = TypeDescriptor.GetConverter(double);
var s = c.ConvertToString(value);

and

var c = TypeDescriptor.GetConverter(double);
return c.ConvertFromString(value.Value);

I get strings with comma separator like 3,33 and conversion back to double works, but will conversion back to double also work in another country which uses dot as separator? (I am in germany, we use comma, but besides that, i want the dot seperator)

Then in another part of the code i convert the doubles in some structs directly by adding it to a XElement:

data.Add(new XElement("x", vector.x));

The XElement constructor converts the double to a string with dot like "3.33".

It gets really strange, when i want to convert this string back to a double:

Double.TryParse(item.Value, out value.x);

and

value.x = Convert.ToDouble(item.Value);

both ignore the dot and i have a double value of 333!?

so i tried it there also with the TypeConverter code, but it gives me an exception, that "3.33" is no valid string for the double converter...

Now what i really need is a number <-> string conversion which always uses dot separator and also parses it righ, regardless the culture/localization settings...

i am lost.. which should i use? i even tried things like:

    NumberFormatInfo provider = new NumberFormatInfo();

    provider.NumberDecimalSeparator = ".";
    provider.NumberGroupSeparator = " ";
    provider.NumberGroupSizes = new int[] { 3 };

    foreach (var item in CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
        item.NumberFormat = provider;
    }

but it didn't help..
thanks for any hint..

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

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

发布评论

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

评论(2

倾城月光淡如水﹏ 2024-09-19 16:53:51

有关如何“修复”此问题的示例。假设这是原始的:

double ParseString(string s)
{
     return double.Parse(s);
}

这是一种修复方法

double ParseString(string s)
{
     return double.Parse(s, CultureInfo.InvariantCulture);
}

,如果您有大量 Parses() ,则可以使用另一种修复方法

double ParseString(string s)
{
     // Save off previous culture and switch to invariant for serialization.
     CultureInfo previousCulture = Thread.CurrentThread.CurrentCulture;
     Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

     double result = double.Parse(s, CultureInfo.InvariantCulture);

     Thread.CurrentThread.CurrentCulture = previousCulture;

     return result;
}

An example on how to "fix" this issue. Assuming this is the original:

double ParseString(string s)
{
     return double.Parse(s);
}

this is one type of fix

double ParseString(string s)
{
     return double.Parse(s, CultureInfo.InvariantCulture);
}

here is another you can use if you have a large number of Parses()

double ParseString(string s)
{
     // Save off previous culture and switch to invariant for serialization.
     CultureInfo previousCulture = Thread.CurrentThread.CurrentCulture;
     Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

     double result = double.Parse(s, CultureInfo.InvariantCulture);

     Thread.CurrentThread.CurrentCulture = previousCulture;

     return result;
}
困倦 2024-09-19 16:53:51

您需要不变的区域性: CultureInfo.InvariantCulture

华泰

You want the invariant culture: CultureInfo.InvariantCulture.

HTH

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