C# 长度计算器英尺和英寸
在我的if语句C# 长度计算器英尺和英寸,c#,console-application,calculator,C#,Console Application,Calculator,在我的if语句(LengthCalculatorOption==1)中,我想将187.96cm转换为英尺和英寸,例如6英尺2英寸。我该怎么做?在我当前的代码中,它显示为6.17英尺,始终为0英寸。我不知道为什么 static void Main(string[] args) { double Centimetres = 0.0, Feet = 0.0, Inches = 0.0; string AnotherConversion = null; string Lengt
(LengthCalculatorOption==1)
中,我想将187.96cm转换为英尺和英寸,例如6英尺2英寸。我该怎么做?在我当前的代码中,它显示为6.17英尺,始终为0英寸。我不知道为什么
static void Main(string[] args) {
double Centimetres = 0.0, Feet = 0.0, Inches = 0.0;
string AnotherConversion = null;
string LengthCalculatorMenu;
int LengthCalculatorOption;
do {
LengthCalculatorMenu = ("Enter 1) Convert centimetres to feet and inches:"
+ "\nEnter 2) Convert feet and inches to centimetres:");
Console.Write(LengthCalculatorMenu);
LengthCalculatorOption = int.Parse(Console.ReadLine());
if (LengthCalculatorOption == 1) {
Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches");
Centimetres = double.Parse(Console.ReadLine());
Feet = (Centimetres / 2.54) / 12;
Inches = (Centimetres / 2.54) - (Feet * 12);
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in feet and inches is {0:C} ft {1:G} ins", Feet, Inches);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else if (LengthCalculatorOption == 2) {
Console.WriteLine("Please Enter the Feet");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter the Inches");
Inches = double.Parse(Console.ReadLine());
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in centimetres is {0:G}cm", Centimetres);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else {
Console.WriteLine("\n\a\t Invalid Option!Option Must be 1 or 2");
}
} while (AnotherConversion == "y" || AnotherConversion == "Y");
试试这个:
Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
inches = (Feet - (double)iFeet) * 12;
double F = Math.Floor(Centimetres * 0.0328084);
Feet = Centimetres * 0.0328084;
Inches = (Feet - F) * 12;
详细说明一下:
您将英尺定义为双精度,这意味着它将是一个十进制值。既然你除以12,它就可以变成十进制
我的代码所做的是将英尺转换为整数(在这种情况下,它将四舍五入为6)。然后我们减去双版本的英尺(在这种情况下为6.17英尺),它等于.17英尺(余数)。我们把它乘以12,从.17英尺换算成英寸
编辑
根据斯科特的评论进行扩展,这将是一种不同的方式
int totalInches = (Centimetres / 2.54); // This will take a floor function of Centimetres/2.54
int Feet = (totalInches - totalInches % 12) / 12; // This will make it divisible by 12
int inches = totalInches % 12; // This will give you the remainder after you divide by 12
将其保持为双精度,使用:
double inp = 12.75; // e.g.
double feet = Math.Floor(inp);
double inches = (inp - feet) * 12.0;
试试这个:
Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
inches = (Feet - (double)iFeet) * 12;
double F = Math.Floor(Centimetres * 0.0328084);
Feet = Centimetres * 0.0328084;
Inches = (Feet - F) * 12;
1英尺=0.30480m要计算厘米到英尺和英寸的值,您可能需要执行以下操作:
double centimeters = 187.96;
double inches = centimeters/2.54;
double feet = Math.Floor(inches / 12);
inches -= (feet*12);
一般来说,一个人应该向下转换到最基本的水平,然后计算你的方式向上。这样,您只需进行一次转换,而不必重复转换计算。在这个例子中,我做了厘米到英寸的简单转换,然后,以英寸为单位计算英尺数,然后从最终的英寸值中减去这么多
所以,如果我有,比如说,38英寸,我会有
Math.Floor(38/12)
英尺,或者3英尺。然后将inches
设置为38-(3*12)
,即2,最终结果为3英尺2英寸。一些通用方法:
public static class Converter
{
public static (int Feet, double Inches) CentimetresToFeetInches(double centimetres)
{
var feet = centimetres / 2.54 / 12;
var iFeet = (int)feet;
var inches = (feet - iFeet) * 12;
return (iFeet, inches);
}
public static string CentimetresToFeetInchesString(double centimetres, string footSymbol = " foot", string inchesSymbol = " inches")
{
(var feet, var inches) = CentimetresToFeetInches(centimetres);
return $"{feet:N0}{footSymbol}, {inches:N0}{inchesSymbol}";
}
}
用法:
(var feet, var inches) = Converter.CentimetresToFeetInches(178);
//feet == 5
//inches == 10.078740157480315
var feetInchesString = Converter.CentimetresToFeetInchesString(178);
//5 foot, 10 inches
就我个人而言,我更喜欢
Math.Floor
而不是自己使用隐式整数trunctation。而且我不喜欢到处都是演员。另外,我觉得自上而下(英尺到英寸)而不是相反的方式会导致精度/价值的潜在损失,尽管我很可能在最后一点上错了@斯科特:你可能在舍入误差方面是对的,修改后的答案可能会起作用,因为你的编辑会产生错误的英尺数。如果totalInches是14,那么(14-(14%12))=14-2=12。@Scott Good catch:)忘了将其除以12,再次修复了它