在数值中设置点而不是逗号

Ksi*_*ice 50 c# xml culture numeric tostring

我有新的XmlDocument对象,在我的程序中创建了ig xml ...

我希望创建的xml中的所有数值都默认使用点符号而不是逗号.

我可以做一次声明它,而不是解析每个十进制值吗?

即在开头的某个地方设置这个点而不是逗号,直到最后都不担心这个?

ban*_*ang 97

试试这个:

System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
Run Code Online (Sandbox Code Playgroud)

  • 为什么不直接在CurrentCulture中分配NumberDecimalSeperator? (2认同)
  • @Brent Visual Studio 显示一个错误,其中显示“非静态字段、方法或属性‘CultureInfo.NumberFormat’需要对象引用。”。因此,需要创建一个实例来访问相关属性。 (2认同)

Tho*_*que 23

您可以使用value.ToString(CultureInfo.InvariantCulture)将数值转换为字符串.或者,您可以将当前文化全局更改为使用点作为小数分隔符的区域性:

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Run Code Online (Sandbox Code Playgroud)


Tig*_*ran 9

使用Decimal.ToString(..)System.Globalization.CultureInfo.InvariantCulture应用的参数.

或者如果你想全球化,请使用

Invariant通过使用Applicaton.CurrentCulture属性将CurrentCulture设置为always .