![](/img/trans.png)
[英]How to change CultureInfo.CurrentCulture in a C#/Windows Store app
[英]I want to change CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator in c#
我正在使用 visual studio 2019。当我使用双打时,它使用“,”而不是“.”。 如果我使用“.”,我会出错。 我四处搜索,我可以更改它,但我想在设置中更改它,以便它自动使用“。” 并不是 ”,”。
这是我使用的代码。
double d = double.Parse(Console.ReadLine());
Console.WriteLine(d);
如果我键入 4.6(带十进制),则会出现此错误。
Unhandled exception. System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Double.Parse(String s)
at project3.Program.Main(String[] args) in C:\Users\a2bme\source\repos\project3\Program.cs:line 9
但 4,6 工作正常。 我使用过其他一些语言并且习惯了“。”。
编辑:
我知道CultureInfo.InvariantCulture
并且想要一种无需每次都使用它的方法。
如果“,”有效且“。” is not valid 一个简单的解决方案可以是这样的。 现在您可以输入 4.5 或 4,5,两者都可以。
using System;
public class Program
{
public static void Main()
{
var input = Console.ReadLine().Replace(".", ",");
double d = double.Parse(input);
Console.WriteLine(d);
}
}
如果您只需要格式化十进制数,请使用
String.Format("{0:n}", 麻木)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.