我对数字有一些问题<->字符串转换我无法解决。
我们编写了一个使用多种 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..
发布评论
评论(2)
有关如何“修复”此问题的示例。假设这是原始的:
这是一种修复方法
,如果您有大量 Parses() ,则可以使用另一种修复方法
An example on how to "fix" this issue. Assuming this is the original:
this is one type of fix
here is another you can use if you have a large number of Parses()
您需要不变的区域性:
CultureInfo.InvariantCulture
。华泰
You want the invariant culture:
CultureInfo.InvariantCulture
.HTH