Feedback Form

Friday, March 14, 2008

Fraction To Decimal

kick it on DotNetKicks.com

I wrote a simple vb.net code to convert from Fraction to decimal easily:

Private Function FractionToDecimal(ByVal frac As String) As String
Dim decimalVal As String = "0"
Dim upper As Decimal = 0
Dim lower As Decimal = 0
Dim remain As Decimal = 0
If frac.IndexOf("/") <> -1 Then


If frac.IndexOf(" ") <> -1 Then
remain = CType(frac.Substring(0, frac.IndexOf(" ")), Decimal)
frac = frac.Substring(frac.IndexOf(" "))
End If

upper = CType(frac.Substring(0, frac.IndexOf("/")), Decimal)
lower = CType(frac.Substring(frac.IndexOf("/") + 1), Decimal)

decimalVal = (remain + (upper / lower)).ToString
End If
Return decimalVal
End Function

3 comments:

mango said...

THANKS - I LIKE IT - HOW ABOUT GOING BACK FROM A DECIMAL TO A FRACTION IN IMPERIAL FORMAT...I.E.

.25 = 1/4
.875 = 7/8

mango said...

HOW ABOUT GOING THE OTHER WAY, I.E.:

.25 = 1/4

5. = 1/2

.5625 = 9/16

etc...

Can you help? I love your posted function on fractions to decimals - thanks -
you saved me!

Amr ElGarhy said...

Already did it but using Javascript on this post http://amrelgarhytech.blogspot.com/2008/03/decimal-to-fraction.html

it may help you.