C# 程序可以';t读取以0.xxx开头的十进制数
我试着做一个简单的公式计算器,但是当我输入十进制以0.xxx开始时,程序只读取xxx。 这是密码C# 程序可以';t读取以0.xxx开头的十进制数,c#,C#,我试着做一个简单的公式计算器,但是当我输入十进制以0.xxx开始时,程序只读取xxx。 这是密码 using System; namespace Tes { class Program { static void Main(string[] args) { double eff, powerf, pow, volt; double result; Console.Write(
using System;
namespace Tes
{
class Program
{
static void Main(string[] args)
{
double eff, powerf, pow, volt;
double result;
Console.Write("Enter the value of voltage ");
volt = int.Parse(Console.ReadLine());
Console.Write("Enter the value of power in kW ");
pow = double.Parse(Console.ReadLine());
Console.Write("Enter the value of efficiency in decimal ");
eff = double.Parse(Console.ReadLine());
Console.Write("Enter the value of power factor ");
powerf = double.Parse(Console.ReadLine());
result = (pow*1000/3) / ((volt/Math.Sqrt(3))* eff * powerf);
Console.WriteLine("The Ampere is " + result);
Console.WriteLine("Volt is " + volt + "\n" + "efficiency is " + eff + "\n"
+ "Power factor is " + powerf);
Console.ReadLine();
}
}
}
结果是哪个是错的。
如何让程序正确读取它?当volt声明为double时,为什么要使用int.Parse?还要使用区域性信息。在德国系统上键入“0.456”(或任何不使用“.”作为十进制分隔符的系统)将不会像预期的那样结束。此外,您的区域性似乎使用逗号作为十进制分隔符,因此请尝试输入“0,87”或在解析方法调用中指定特定的区域性。我在处理数据类型时忘记了编辑该区域性(尝试将双精度更改为十进制等)事实证明,问题在于分隔符(我的系统使用“,”)。感谢您的帮助:)您可以在应用程序开始时使用
Thread.CurrentThread.CurrentCulture=System.Globalization.CultureInfo.InvariantCulture更改区域性代码>。出于某种原因,CurrentUICulture
在控制台应用程序中无效,但CurrentUICulture
有效。