Question

In my if statement (LengthCalculatorOption == 1), I want to convert, for example, 187.96cm to feet and inches, such as 6feet 2ins. How do I do that? In my current code, it shows 6.17feet and always 0ins. I have no idea why.

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");
Was it helpful?

Solution

Try this:

Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
inches = (Feet - (double)iFeet) * 12;

To elaborate a bit:

You are defining feet as a double, which means that it will be a decimal value. So since you're dividing by 12, it can become a decimal representation.

What my code does is it converts Feet to integer (which will round it to 6 in this situation). We then subtract the double version of Feet (6.17 in this situation) which equals .17 (The remainder). We multiply that by 12 to convert from .17 feet to inches

Edit

To expand based on Scott's comment, this would be a different way to go

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

OTHER TIPS

To calculate a value in centimeters into feet and inches, you'd likely want to do this:

double centimeters = 187.96;
double inches = centimeters/2.54;
double feet = Math.Floor(inches / 12);
inches -= (feet*12);

Generally-speaking, one should convert down to the most basic level, then calculate your way up. This way, you only do the work of converting one time, instead of having to repeat the conversion calculation. In this insntance, I do the simple conversion of centimeters to inches, and then after that, count the number of feet in inches, then subtract that much from the final inches value.

So, if I had, say, 38 inches, I would have Math.Floor(38 / 12) feet, or 3. Then inches would be set to 38 - (3*12), which is 2, giving a final result of 3 feet, 2 inches.

Keeping it as double, use:

double inp = 12.75; // e.g.

double feet = Math.Floor(inp);
double inches = (inp - feet) * 12.0;

Try this:

 double F = Math.Floor(Centimetres * 0.0328084);
 Feet = Centimetres * 0.0328084;
 Inches = (Feet - F) * 12;

1 ft = 0.30480m

Some general-purpose methods:

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}";
    }
}

Usage:

(var feet, var inches) = Converter.CentimetresToFeetInches(178);
//feet == 5
//inches == 10.078740157480315

var feetInchesString = Converter.CentimetresToFeetInchesString(178);
//5 foot, 10 inches
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top