TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert Feet, Inches

This C# program converts figures in feet and inches. This method can be used for displaying heights.

Convert feet, inches. Feet and inches represent height.

You have a number in inches and you want to represent it in feet and inches in your C# program. This is conventional in some countries for heights. For example a person would be 5 foot 10.

Example. First, if you have the data stored in inches in your program, it is probably best to just leave it in inches and use this method only for display purposes. A single unit is simpler to store.

The ToFeetInches method receives a double of the number of total inches. The feet part is the Key of a KeyValuePair. And the inches part is the Value. The feet is an integer computed by dividing by 12.

KeyValuePair

And: The inches part is computed with modulo division. We determine the remainder after dividing by 12.

C# program that converts to feet and inches

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	var result = ToFeetInches(70.0);
	Console.WriteLine(result);

	result = ToFeetInches(61.5);
	Console.WriteLine(result);

	result = ToFeetInches(72.0);
	Console.WriteLine(result);
    }

    static KeyValuePair<int, double> ToFeetInches(double inches)
    {
	return new KeyValuePair<int, double>((int)inches / 12, inches % 12);
    }
}

Output

[5, 10]
[5, 1.5]
[6, 0]

In this program, you can see the results of this program for possible heights of people. 70 inches is changed to 5 feet and 10 inches. 72 inches is changed to 6 feet and zero inches.

Discussion. This sort of method is best reserved for display purposes. Storing figures such as heights would be best done with a single value in a database. But if users expect a certain notation, it is friendly to display it that way.

So: Instead of 70 inches, you could display 5 foot 10. This is more familiar for some users.

Summary. We used division, casting and modulo division to convert a figure in inches to feet and inches in the C# language. We used the KeyValuePair as a return type, allowing us to return two values in a struct.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf