Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate fraction from decimal
Message
From
10/03/2010 07:43:26
 
 
To
09/03/2010 19:54:33
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2008 Server
Database:
MySQL
Application:
Desktop
Miscellaneous
Thread ID:
01453419
Message ID:
01453685
Views:
49
This definitely helps, thank you for your work.

>Hi Christian.
>
>Here's some code I just whipped up. You just need to pass it the decimal value and an optional accuracy and it will return a string containing a fraction that, when converted back to a decimal value, will be within the accuracy limit of the original value. If you don't pass an accuracy then it defaults to .0001 ( one in ten thousand). This code only handles the fractional part of the decimal, though it's easy enough to modify to handle the whole number if you want to. I didn't bother since your examples were only fractional parts and I dn't know how you'd want to display it. For Example, is 1.5 to be 1+1/2 or is it 3/2. If you need it to handle the non fractional part of the number too and can't work it out, let me know how you want it displayed and I'll be happy to change it.
>
>
>Function CalcFraction
>Lparameters tnDecimal, tnAccuracy
>
>Local lnDecimal, lnAccuracy, lnNumerator, lnDenomonator
>
>lnDecimal = Abs(M.tnDecimal)
>lnAccuracy = Iif(Pcount() < 2, 0.0001, M.tnAccuracy)
>lnNumerator = 1
>lnDenominator = Int(1 / M.lnDecimal)
>
>Do while Abs(M.lnDecimal - M.lnNumerator / M.lnDenominator) > M.lnAccuracy
>	If M.lnDecimal < M.lnNumerator / M.lnDenominator
>		lnDenominator = M.lnDenominator + 1
>	Else
>		lnNumerator = M.lnNumerator + 1
>	EndIf
>EndDo
>
>Return Transform(Sign(M.tnDecimal) * M.lnNumerator) +"/"+ Transform(M.lnDenominator)
>
>
>I hope that helps.
>
>Ian Simcock.
>
>
>
>>Hello,
>>
>>Does somebody know how to calculate a fraction from a decimal?
>>
>>I have the user input 0.75 and the program should generate "3/4".
>>
>>I would highly appreciate your help.
Christian Isberner
Software Consultant
Previous
Reply
Map
View

Click here to load this message in the networking platform