Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert a Decimal to a Fraction
Message
De
17/02/2001 08:18:59
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Convert a Decimal to a Fraction
Divers
Thread ID:
00476976
Message ID:
00476976
Vues:
57
After much help from George Tasker and others yesterday I have come up with the code that solves my problem of converting the decimal part of a number to a fraction. In my case the decimal part came from a fraction originally but because of rounding will not be the exact fraction when converted back. This is for a measurment like from a tape measure.
Therefor the code that worked for my application is:
LPARAMETER lcString
LOCAL lnNum, lnDem
* Pass the Decimal part only of the String ie. ".125"
* If 64 is the largest denominator then .9921 is the 
* largest decimal that will return less than 1
lnDem = 64  && Largest Denominator to start with
lnNum = ROUND((VAL(lcString) * lnDem), 0)  && Calculate the Numerator
* Reduce to lowest possible Denominator
DO WHILE MOD(lnNum,2) = 0 AND MOD(lnDem,2) = 0
  lnNum = lnNum / 2
  lnDem = lnDem / 2
ENDDO
RETURN ALLTRIM(STR(lnNum)) + "/" + ALLTRIM(STR(lnDem))+["]
This and everything else I know is in my VFP Programmers Help File on my web site.
http://www.semo.net/suburb/thornton/visual.html

Enjoy!
Répondre
Fil
Voir

Click here to load this message in the networking platform