![](/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.