Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Ok, I am trying to do something real simple is to convert feet,inch,part back to

Posted on 2014-04-14
Medium Priority
230 Views
x=44' 4" 3/16
y=24' 4" 7/16

Diagonal=sqrt( x*x+y*y); only a formula

I converted  ft*12+inches+fraction and then did the math on them

I want to display the result in the ft inch fraction format.

I did feet= result/12
inches = result % 12

not sure how to display the fraction.

http://aceyourplace.com/aypv1/Prod/CalculateDiagonal.aspx
CalculateDiagonal.aspx.cs
CalculateDiagonal.aspx
0
Question by:goodk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 9
• 3
• 2
• +2

LVL 75

Expert Comment

ID: 39999447
Well, result % 12 should result in a double (or float), the whole part of which would be the whole inches; the decimal part being the fractional inches. As to converting a decimal value to a fraction...that will be a bit harder to do. When you do so, you have to be able to detect things like non-terminating values--which you really can't do. Now if you can round off to a certain decimal place, then you would have an easier time hacking out a fraction converter. You'd have a little bit of work in doing simplification, though.
0

Author Comment

ID: 39999475
Well, is there a simple way to use the select list to get the next greater fraction.

First how do I fetch .7 out of 3.97, if rounded to two decimals?

next how to use select list to get the next greater fraction.
0

LVL 75

Expert Comment

ID: 39999482
First how do I fetch .7 out of 3.97, if rounded to two decimals?
One approach:

``````double x = 3.97;
double whole = Math.Truncate(x);
double part = x - whole;
``````

next how to use select list to get the next greater fraction.
Select list?
0

LVL 10

Expert Comment

ID: 39999502
you're CLOSE, but missing just two steps I think.

feet= result/12
inches = result % 12

This is good, but will give you feet AND inches in the form of 9.5 or 7.75 - a decimal point: which is close to but I think, not what you want.

1)
You will need to determine the number of 16th
sixteenths = inches % 16

2)
The last step is to drop any remaining decimals from feet and inches (probably sixteenths too- though this could cause loss of accuracy)
This will truncate off those decimals:
feet=(int) feet
inches = (int) inches
sixteeths = (int) sixteenths

NOTE: When you display the numbers, you will need to concatenate the hard coded "/16" part of the fraction (divisor).  (the numerator for this fraction is the "sixteenths", you compute earlier)  Let me know if you need more help with this last part: putting the string together.
0

LVL 40

Expert Comment

ID: 39999603
Sorry, everything is in French, but here is a VB function to convert a Double into a String that display a fraction. It should be easy to convert it to C#.

Simply pass the Double, the precision (ex: 16 to get the closest fraction in 1/16), and a Boolean that you set to True if you want to return a single character for fractions that have a Unicode representation.

``````	Public Function ToFraction(ByVal value As Double, ByVal precision As Integer, ByVal singleChar As Boolean) As String

'Convertit une valeur décimale en fraction
'Algorithme original fourni par sebastien.laroche@itc.alstom.com
'
'Params : value				Valeur à convertir (fournie par l'objet sur lequel on appelle la méthode)
'					precision		Précision (Ex: 32 pour 1/32)
'					singleChar	Passer True pour avoir 1/4, 1/2 et 3/4 en un seul caractère (¼, ½ et ¾)

Dim résultat As String = String.empty
Dim dividende As Integer
Dim diviseur As Integer

'Partie entière de la mesure
If value >= 1 Then
résultat = CStr(Math.Truncate(value))
value = Math.Abs(value - Math.Truncate(value))
End If

If value = 0 Then

'Pas de fraction
Return résultat

Else

'Tronque les valeurs plus petites que la précision
If value <= (1 / precision / 2) Then
Return résultat
End If

'Fabrique la fraction brute avec le restant
dividende = CInt(value * precision)
diviseur = precision

'Trouver la forme la plus simplifiée
Do While dividende Mod 2 = 0
dividende = dividende \ 2
diviseur = diviseur \ 2
Loop

End If

If singleChar Then
'Détecte les fractions définies en ASCII/Unicode
Select Case dividende & diviseur
Case "14" : Return résultat & " ¼"
Case "12" : Return résultat & " ½"
Case "34" : Return résultat & " ¾"
End Select
End If

Return (résultat & " " & dividende & "/" & diviseur).Trim

End Function
``````
0

LVL 3

Expert Comment

ID: 39999757
I'd take a slightly different approach to getting the fraction:

``````private string GetNonDecFraction(single value)
{
single whole = Math.Floor(value);
single fractValue = value - whole;
//If there is no fractional value return whole number and terminate
if (fractValue == 0)
{
return whole.ToString();
}
single dividend = 99;
single remainder = 99;
single fraction = 99;
single divisor = 99;

for (int i = 5; i > 0; i--)
{
divisor = 2^i;
if (divisor % fractValue <= remainder)
{
remainder = divisor % fractValue;
fraction = divisor;
dividend - Math.Floor(fractValue/divisor);
}
}
string nonDecFraction = dividend.ToSTring() + "/" + divisor.ToString();
string endResult = "99";
if (whole == 0)
{
if (dividend == 0)
{
endResult = "0";
}
else
{
endResult = nonDecFraction;
}
}
else
{
endResult = whole.ToString() + " " + nonDecFraction;
}
return endResult;
}
``````

It iterates backwards from 32nds - or 1/(2^5)  - to halves - 1 (2^1) - and keeps the values with the lowest remainder.  I chose 32nds as the smallest fraction because that's the lowest fraction commonly used with inches but you could easily change this by increasing or decrease the upper limit of the for loop. For example if you wanted quarter inches to be your smallest fraction you could change the for loop to for (int i = 2; i > 0; i--) or got 128ths to for (int i = 7; i > 0; i--) and so on.
0

Author Comment

ID: 40000770
So far this is what I have,

Double Length = Convert.ToDouble(LengthFt.Text) * 12.0 + Convert.ToDouble(LengthIn.Text) + Convert.ToDouble(Length16.Text);
Double Width = Convert.ToDouble(WidthFt.Text) * 12.0 + Convert.ToDouble(WidthIn.Text) + Convert.ToDouble(Width16.Text);
Double Diagonal = Math.Sqrt((Length * Length) + (Width * Width));
DiagonalFt.Text = Convert.ToString( (int) (  (Diagonal / 12)  ));
DiagonalIn.Text = Convert.ToString( (int) (  (Diagonal % 12)) );

Diagonal16.Text = Convert.ToString( (int) (  (Diagonal % 12) ) % 16 );

Peter,
What do I need to pass to the function you have provided?
0

LVL 3

Expert Comment

ID: 40000823
You can pass any of your values in inches to get back a string representing inches and the closest fractional match for fractions of an inch.

For example if you pass it 9.25 you should get back "9 1/4" or for 3.375 you'd get back "3 3/8"
0

Author Comment

ID: 40001548
I changed single to double is that right for c#?  I keep getting Infinity/number?

try
http://aceyourplace.com/aypv1/Prod/CalculateDiagonal.aspx

Help. thanks

protected void Button1_Click(object sender, EventArgs e)
{
Double Length = Convert.ToDouble(LengthFt.Text) * 12.0 + Convert.ToDouble(LengthIn.Text) + Convert.ToDouble(Length16.Text);
Double Width = Convert.ToDouble(WidthFt.Text) * 12.0 + Convert.ToDouble(WidthIn.Text) + Convert.ToDouble(Width16.Text);
Double Diagonal = Math.Sqrt((Length * Length) + (Width * Width));
DiagonalFt.Text = Convert.ToString( (int) (  (Diagonal / 12)  ));
DiagonalIn.Text = Convert.ToString(GetNonDecFraction(Diagonal % 12));
//    DiagonalIn.Text = Convert.ToString( (int) (  (Diagonal % 12)) );
//    Diagonal16.Text = Convert.ToString( (int) (  (Diagonal % 12) ) % 16 );
}

private string GetNonDecFraction(double value)
{
double whole = Math.Floor(value);
double fractValue = value - whole;
//If there is no fractional value return whole number and terminate
if (fractValue == 0)
{
return whole.ToString();
}
double dividend = 99;
double remainder = 99;
double fraction = 99;
double divisor = 99;

for (int i = 5; i > 0; i--)
{
divisor = 2 ^ i;
if (divisor % fractValue <= remainder)
{
remainder = divisor % fractValue;
fraction = divisor;
dividend = Math.Floor(fractValue / divisor);
}
}
string nonDecFraction = dividend.ToString() + "/" + divisor.ToString();
string endResult = "99";
if (whole == 0)
{
if (dividend == 0)
{
endResult = "0";
}
else
{
endResult = nonDecFraction;
}
}
else
{
endResult = whole.ToString() + " " + nonDecFraction;
}
return endResult;
}
0

LVL 3

Expert Comment

ID: 40002066
keep forgetting that "^" is XOR in C# instead the exponent operator plus clean up a couple of errors.  Give this a try

``````private string GetNonDecFraction(double value)
{
double whole = Math.Floor(value);
double fractValue = value % whole;
//If there is no fractional value return whole number and terminate
if (fractValue == 0)
{
return whole.ToString();
}
double dividend = 99;
double remainder = 99;
double fraction = 99;
double divisor = 99;

for (int i = 1; i <= 5; i++)
{
divisor = Math.Pow(2,i);
if (fractValue % (1/divisor) < remainder)
{
remainder = fractValue % (1/divisor);
fraction = divisor;
dividend = Math.Floor(fractValue / (1/divisor));
}
}
string nonDecFraction = "99";
if (dividend == 0)
{
nonDecFraction = "";
}
else
{
nonDecFraction = dividend.ToString() + "/" + divisor.ToString();
}
string endResult = "99";
if (whole == 0)
{
if (dividend == 0)
{
endResult = "0";
}
else
{
endResult = nonDecFraction;
}
}
else
{
endResult = whole + " " + nonDecFraction;
}
return endResult;
}
``````
0

Author Comment

ID: 40002278
tried 65 5 1/2
and 32 5 1/2

gives 73 99/32
0

Author Comment

ID: 40002356
The answer should be 73' 49/64" or 3/4
0

Author Comment

ID: 40002437
.7670  it is not converting it correctly.  I tried to debug but first loop

if (fractValue % (1 / divisor) < remainder) is never true so the values stays 99
0

Author Comment

ID: 40003912
? waiting for a response
0

Author Comment

ID: 40005482
double whole = Math.Floor(value);
double fractValue = value % whole;

fractValue becomes NaN as whole returns a 0.0

Was your program working fine in VB?
0

LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40005489
The code I sent you in VB and the code aces sent you in C# are not the same.

Mine does work in VB, I use it every day in one of my current project that is an application for woodworkers. Unfortunately, I do not have the time to translate it to C#.
0

Author Closing Comment

ID: 40007432
I changed the code from vb to c# and  JamesBurger solution worked correct the first time.  Thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month6 days, left to enroll