[英]I want to change CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator in c#
I am using visual studio 2019. When I use doubles, it uses "," instead ".".我正在使用 visual studio 2019。当我使用双打时,它使用“,”而不是“.”。 I get error if I use ".".
如果我使用“.”,我会出错。 I searched around and I can change it but I want to change it inside setting so that it automatically uses "."
我四处搜索,我可以更改它,但我想在设置中更改它,以便它自动使用“。” and not ",".
并不是 ”,”。
this is code i use.这是我使用的代码。
double d = double.Parse(Console.ReadLine());
Console.WriteLine(d);
and if i type 4.6 (decimal with.) it gets this error.如果我键入 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
but 4,6 works fine.但 4,6 工作正常。 I have worked with some other languages and am used to ".".
我使用过其他一些语言并且习惯了“。”。
edit:编辑:
I know about CultureInfo.InvariantCulture
and want a way without using it every time.我知道
CultureInfo.InvariantCulture
并且想要一种无需每次都使用它的方法。
If "," is valid and "."如果“,”有效且“。” is not valid A simple solution can be this.
is not valid 一个简单的解决方案可以是这样的。 Now you can enter 4.5 or 4,5 and both will work.
现在您可以输入 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);
}
}
If you only need to format the decimal number use如果您只需要格式化十进制数,请使用
String.Format("{0:n}", numb) String.Format("{0:n}", 麻木)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.