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:
00476910
Views:
37
Hi George,

Dagnabit... I was passing it as a text string not a number.... didn't notice the n in your parameter....

Last minutes thoughts on my code I can loose the first half and it will work ok. Gotta round the decimal to the nearest fraction...

Thanks for the input and help...

>Hi John,
>>
>>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.
>
>You did??? I ran for every possible fractional value in 4ths, 8ths, 16ths, 32nds and 64ths and it worked fine. I just repeated the test and it still works fine. As I mentioned, it only works on fractions where the denominator is a power of two. I'd be interested in hearing what the problem was.
>
>>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!!!
>
>The same to you, John.
Previous
Reply
Map
View

Click here to load this message in the networking platform