Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert Fractions to Decimal
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00476506
Message ID:
00476811
Vues:
26
Hi George,

I had an error on your code don't have any more time this afternoon to look at it. Thanks so much for working on it.

What I ran into with my code is the varing precision of the decimal conversions of fractions ie. 1/64" = 0.015625" 1/2" = 0.5" etc.

As my incoming code rounds to the set decimal so have to round back in the fraction converter...

The code now that works with 0.015" = 1/64" etc.
LPARAMETER lcString
LOCAL lnNum, lnDom, lnMax, lnDecimal, lnPower, llSucess
* Decimal part only of the Fraction
lnMax = 64
lnNum = 1
lnDom = 2
lnPower = 1
llSucess = .F.
lnDecimal = VAL(lcString)
DO WHILE lnDom <= lnMax
  lnDom = 2^lnPower
  lnNum = lnDecimal * lnDom
  IF ROUND(lnNum,0) == lnNum
    llSucess = .T.
    EXIT
  ENDIF
  lnPower = lnPower + 1
ENDDO
IF NOT llSucess && Round to nearest fraction
  lnNum = ROUND((lnDecimal * lnMax), 0)
  lnDom = lnMax
  DO WHILE MOD(lnNum,2) = 0 AND MOD(lnDom,2) = 0
    lnNum = lnNum / 2
    lnDom = lnDom / 2
  ENDDO
ENDIF
RETURN ALLTRIM(STR(lnNum)) + "/" + ALLTRIM(STR(lnDom))+["]
Have a good WeekEnd!!!
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform