Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calculate fraction from decimal
Message
De
09/03/2010 19:54:33
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2008 Server
Database:
MySQL
Application:
Desktop
Divers
Thread ID:
01453419
Message ID:
01453653
Vues:
89
This message has been marked as a message which has helped to the initial question of the thread.
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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform