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:
00476789
Vues:
29
>Hey George
>
>Her is one that I think is better...
>
>
>LPARAMETER lcString
>LOCAL lnNum, lnDom, lnMax, lnDecimal, lnPower
>lnMax = 64
>lnNum = 1
>lnDom = 2
>lnPower = 1
>lnDecimal = VAL(lcString)
>* Decimal part only
>DO WHILE lnDom <= lnMax
>  lnDom = 2^lnPower
>  lnNum = lnDecimal * lnDom
>  IF ROUND(lnNum,0) == lnNum
>    EXIT
>  ENDIF
>  lnPower = lnPower + 1
>ENDDO
>RETURN ALLTRIM(STR(lnNum)) + "/" + ALLTRIM(STR(lnDom))+["]
>
>
>Still need to take into consideration rounding for a generic function
>
>TGIF
>
>Thank George it's Friday

< beg >Really, John, the only thing that matters is that it works.< s > I came up with my own version. If you interested
FUNCTION Dec2Fraction

  LPARAMETER tnDec
  
  LOCAL lcresult, lnpt, lnnum, lcdec, lnexp,;
    lndenom, lcdenom
  lcresult = ""
  lcdec = TRANSFORM(tnDec)
  lnpt = AT(".", lcdec)
  IF lnpt > 0
    lnexp = LEN(SUBSTR(lcdec, lnpt + 1))
    lndenom = 2 ^ lnexp
    lnnum = 0
    DO WHILE lnnum / lndenom # tnDec
      lnnum = lnnum + 1
    ENDDO
    lcresult = TRANSFORM(lnnum) + "/" + TRANSFORM(lndenom)
  ENDIF
  RETURN lcresult
ENDFUNC
This only works for fractions where the denominator is a power of 2.
George

Ubi caritas et amor, deus ibi est
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform