简体   繁体   English

我想在 c# 中更改 CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

[英]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.

相关问题 如何在C#/ Windows应用商店应用中更改CultureInfo.CurrentCulture - How to change CultureInfo.CurrentCulture in a C#/Windows Store app C#:更改正则表达式的NumberDecimalSeparator - C#: Change NumberDecimalSeparator for Regex 等同于CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol的文本 - Text equivalent of CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol 更改CultureInfo C# - Change CultureInfo c# CultureInfo.CurrentCulture不包含正确的值->数字格式问题 - CultureInfo.CurrentCulture does not contain the correct value --> numberformat problems 如何在 KeyDown 事件 (C#) 中检测 NumberDecimalSeparator - How do I detect a NumberDecimalSeparator in a KeyDown event (C#) C#CultureInfo.CurrentCulture说en_US但我的Windows设置设置为南非 - C# CultureInfo.CurrentCulture says en_US but my Windows settings are set to South Africa TimeSpan.ParseExact(“ 000000.000”,“ hhmmss.fff”,CultureInfo.CurrentCulture); 输入的字符串格式不正确C#? - TimeSpan.ParseExact(“000000.000”, “hhmmss.fff”, CultureInfo.CurrentCulture); Input string was not in a correct format C#? 为什么C#CultureInfo.CurrentCulture在Window-7日本文化中赋予en_US? - Why C# CultureInfo.CurrentCulture give en_US in Window-7 Japanese culture? 选择语言并在MVC / C#中更改CultureInfo - Select language and change the CultureInfo in MVC / C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM