Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用“输入”输入的相同区域性中显示输出;Parse(str,CultureInfo.InvariantCulture)&引用;?_C#_.net_Windows_Visual Studio - Fatal编程技术网

C# 如何在使用“输入”输入的相同区域性中显示输出;Parse(str,CultureInfo.InvariantCulture)&引用;?

C# 如何在使用“输入”输入的相同区域性中显示输出;Parse(str,CultureInfo.InvariantCulture)&引用;?,c#,.net,windows,visual-studio,C#,.net,Windows,Visual Studio,我对C.,.NET和Windows上的编程基本上都是新手。 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Globalization; namespace MyApp { class Program { static void Main(

我对
C.
.NET
和Windows上的编程基本上都是新手。 这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string lineBreak = "\r\n";
            string str = Console.ReadLine();
            double number = double.Parse(str, CultureInfo.InvariantCulture);
            Console.WriteLine("You've entered: " + number + lineBreak + "Variable type:" +number.GetType());
            Console.WriteLine("Press any key to exit . . .");
            Console.Read();
        }
    }
}
如果像这样输入
double
“32,45”(注意,有一个逗号作为分隔符),则得到如下输出:

You've entered: 32,45
Variable type: System.Double
You've entered: 32,45
Variable type: System.Double
但是当我输入像这样的
double
变量
“32.45”
(注意,这里有点而不是逗号)时,我得到如下输出:

You've entered: 32,45
Variable type: System.Double
You've entered: 32,45
Variable type: System.Double
都一样。我知道这是因为本地机器上设置了CultureInfo

我如何使它独立(不改变我的设置或东西,但完全在我的代码中?所以当用户打开我的程序时,他不应该改变他的设置中的任何东西)

比如当我输入dot时,它输出双精度点,当我输入逗号时,它显示逗号


最好的问候,pāvels。

按照您现在编写代码的方式,它将无法解析以逗号作为小数分隔符的数字。NET Framework不直接支持解析这两种样式,因为这是不明确的。
double.Parse(“32,45”,CultureInfo.InvariantCulture)
outputs
处理用户输入或输出时,请始终使用默认的本地区域性。当处理其他进程/服务器时,总是使用不变的文化。我还在学习,谢谢你们的建议,伙计们@TimSchmelter不知道为什么,但我看到的输出与我在这个问题中所写的相同。我正在学习教程,在教程中有一种不同的输出(有我想要实现的那种输出)。@RichardSchneider:这太笼统了。如果您的用户被指示使用特定的格式,那么这就是您应该使用的格式,如果您知道服务器/进程的格式,那么就使用该格式,而不是
InvariantCulture