BelieveMe
BelieveMe

Reputation: 69

How to convert meters into feet and inches

Creating a program and I have been struggling to convert metres to feet and inches, but I finally got it working I think.

My issue now is with the variable inchesleft it is a int and I am struggling to work out how to make it an integer as I want to drop the remainder of the inches value so I can get a value of 6feet 4inches etc.

code below:

double inft, convert, inchesleft, value = 0.3048;
int ft;
string input;

Console.WriteLine("please enter amount of metres");
input = Console.ReadLine();
convert = double.Parse(input);

inft = convert / value;
ft = (int)inft;
inchesleft = convert / value % 1 *12;


Console.WriteLine("{0} feet {1} inches.", ft, inchesleft);
Console.ReadLine();

Upvotes: 4

Views: 14302

Answers (2)

Kerry7777
Kerry7777

Reputation: 4574

I used some parts of @Waqar Ahmed method above & tweaked it slightly. Thanks man.

//-----METHODS
        //VARIABLES
        double inchFeet;
        int wholeFeet;

    public double GetHeightFeet()
    {
        //CENTIMETERS TO FEET
        //PlayerHeight is ___cm input
        inchFeet = (PlayerHeight / 0.3048) / 100;
        //LEFT PART BEFORE DECIMAL POINT. WHOLE FEET
        wholeFeet = (int)inchFeet;
        return wholeFeet;
    }

    public double GetHeightInches()
    {
        //DECIMAL OF A FOOT TO INCHES TEST HEIGHT 181cm to see if 11''
        double inches = Math.Round((inchFeet - wholeFeet) / 0.0833);
        return inches;
    }

Upvotes: 0

Waqar Ahmed
Waqar Ahmed

Reputation: 1444

Try this:

double inft, convert, value = 0.3048;
int ft, inchesleft;
string input;

Console.WriteLine("please enter amount of metres");
input = Console.ReadLine();
convert = double.Parse(input);

Divide the input number by 0.3048 to get Feet

inft = convert / value;

Now we got Feet in decimal. Fetch the left part of feet (before decimal point)

ft = (int)inft;

Fetch the right part of Feet (after decimal point) and divide it by 0.08333 to convert it into Inches

double temp = (inft - Math.Truncate(inft)) / 0.08333; 

Now we got inches in decimal. Fetch the left part of Inches (before decimal point)

inchesleft = (int)temp; // to be more accurate use temp variable which contains the decimal point value of inches 

Console.WriteLine("{0} feet {1} inches.", ft, inchesleft);
Console.ReadLine();

Upvotes: 6

Related Questions