Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert Fractions to Decimal
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00476506
Message ID:
00476632
Views:
28
Hi Barbara,

No, I'm not kidding....

Here is the code I have so far
LPARAMETERS lpString, lcInch, lcNum, lcDen
LOCAL llRet
lnRet = 0
lcInch = ''
lcNum = ''
lcDen = ''
lpString = ALLTRIM(lpString)
DO CASE
CASE OCCURS("'", lpString) = 1 && Foot Symbol
  lnRet = VAL(ALLTRIM(LEFT(lpString, AT("'", lpString) - 1)))
  lpString = ALLTRIM(SUBSTR(lpString, AT("'", STRTRAN(lpString, '"', '')) + 1))
  * Get inches
  DO WHILE ISDIGIT(lpString)
    lcInch = lcInch + LEFT(lpString,1)
    lpString = SUBSTR(lpString,2)
  ENDDO
  lnRet = lnRet + (VAL(lcInch) / 12)
  * Get Fractions
  lpString = ALLTRIM(lpString)
  IF OCCURS([/], lpString) > 0  && we got a fraction
    DO WHILE ISDIGIT(lpString)
      lcNum = lcNum + LEFT(lpString,1)
      lpString = SUBSTR(lpString,2)
    ENDDO
    lpString = STRTRAN(lpString, [/], '')
    DO WHILE ISDIGIT(lpString)
      lcDen = lcDen + LEFT(lpString,1)
      lpString = SUBSTR(lpString,2)
    ENDDO
    lnRet = lnRet + ((VAL(lcNum) / VAL(lcDen))/ 12)
  ENDIF
CASE OCCURS(["], lpString) = 1 && Inch Symbol only
  DO WHILE ISDIGIT(lpString)
    lcInch = lcInch + LEFT(lpString,1)
    lpString = SUBSTR(lpString,2)
  ENDDO
  lnRet = lnRet + (VAL(lcInch) / 12)
  * Get Fractions
  lpString = ALLTRIM(lpString)
  IF OCCURS([/], lpString) > 0  && we got a fraction
    DO WHILE ISDIGIT(lpString)
      lcNum = lcNum + LEFT(lpString,1)
      lpString = SUBSTR(lpString,2)
    ENDDO
    lpString = STRTRAN(lpString, [/], '')
    DO WHILE ISDIGIT(lpString)
      lcDen = lcDen + LEFT(lpString,1)
      lpString = SUBSTR(lpString,2)
    ENDDO
    lnRet = lnRet + ((VAL(lcNum) / VAL(lcDen))/ 12)
  ENDIF
  IF OCCURS([.], lpString) > 0  && we got a decimal
    lpString = STRTRAN(lpString, [.], '')
    lnRet = lnRet + (VAL(lpString) / 12)
  ENDIF
OTHERWISE  && Feet Only
  lnRet = VAL(ALLTRIM(lpString))
ENDCASE


RETURN lnRet
>>Does anyone have code to convert a fraction to a decimal and from decimal to a fraction?
>>
>>
>>Thanks
>
>You're kidding about that first part, right? <s> I mean to convert any fraction, you just type that fraction, i.e. 1/2, 1/3, 4/5, etc. and it returns the decimal (based on the setting for SET DECIMALS TO.) After all, it's just an expression.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform