# Display a value as feet and inches

OK, all you American types

How can I display a value in decimal feet as â€śFeet and Inchesâ€ť. eg 8â€™ 7/16"

I am making an oil tank monitoring system for a friend who has a small oil well lease in Texas and he wants to use Feet and Inches as the display option so as not to confuse his local guy.

The software actually has Meters, Feet and Inches as display options (why everyone is not all metricated these days sure makes our programming tasks more elaborate)

I am trying to come up with a way to display the reading but getting strange things like 2/5" which is not ideal. Iâ€™d like to round this to the nearest 1/16"

Converting back from feet and inches input was easy enough.

Would they not be happy with feet+decimal_inches? Working out fractional inches is a real pain. I donâ€™t think I have ever seen this on a computer program. How do you decide what the denominator should be! You would have to have a list of preferred values and select the one that gave the most accurate result.

The former is very similar to converting decimal latitudes to degree+minutes+seconds. There are loads of examples of this on the net.

This works for me.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoxCalculator
{
public static class Helpers
{
#region Helpers

public static string ConvertMmToInches(double mm)
{
string inches = string.Empty;

double totalInches = mm / 25.4;

double wholeInches = Math.Truncate(totalInches);

double fractionalInches = totalInches - wholeInches;

double eighthInches = Math.Round(fractionalInches * 8, 0);

if (eighthInches == 8)
{
eighthInches = 0;
wholeInches++;
}

int gcd = GCD((int)eighthInches, 8);

double normalizedInches = eighthInches / (double)gcd;

if (eighthInches == 0)
{
inches = wholeInches.ToString();
}
else
{
// Normaise the fraction
inches = wholeInches.ToString() + " " + normalizedInches.ToString() + "\\" + (8 / gcd).ToString();
}

return inches;
}

public static int GCD(int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}

#endregion
}
}

``````
2 Likes

I have it as decimal feet and inches already but the client asked for it to be shown as feet and fraction of inches. The values internally are all decimal, it was just the display he wanted changed.

Iâ€™ll try the code Jason posted.

@ Dave McLaughlin - Dave, itâ€™s used here in an asp.net page.

http://thecraftyowl.co.uk/boxbuster/

In case you wanted to see it working.

Cheers.

@ Jason. I am getting an error with Math.Round and it states there is no overloaded option that takes to parameters. Round for me only takes 1 parameter.

Is that code full NET or NETMF?

I also donâ€™t have the System.Linq or System.Collections.Generic that shows in your listing.

@ Dave McLaughlin - Itâ€™s full .NET, not NETMF. I quickly grabbed it from my NAS and pasted it. You wonâ€™t need

``````
using System.Collections.Generic;
using System.Linq;

``````

and

``````
double eighthInches = Math.Round(fractionalInches * 8, 0);

``````

simple rounds to no decimal places after multiplying my 8, and I think thatâ€™s what the netmf implementation o Math.Round(double) does.

Thanks Jason,

The code was a good start and as I needed FEET and INCHES displayed, I reworked it and I know have a reading to the nearest 1/4". 1/16" was just too fine.

@ Dave McLaughlin - Youâ€™re welcome.

@ Jason - This is very useful! Please add it to CodeShare so it doesnâ€™t get lost. Thanks!

1 Like

@ jasdev - Will Do.

@ jasdev - Here ya go.

https://www.ghielectronics.com/community/codeshare/entry/922

Thanks Jason!

Iâ€™ll tidy up what I have and post this later too.

It doesnâ€™t do the conversion, it simply takes a decimal feet value and converts this to a string that is in feet, inches and fractional.