Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert string 1/4 or 1/2 to numeric
Message
From
18/01/2016 03:16:30
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
 
 
To
17/01/2016 21:22:03
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01629854
Message ID:
01629863
Views:
65
>>>Is there a way to convert the string that has a slash to a numeric (without parsing the string)? For example, make "1/4" to .25, "1/2" to ".5" and so one?
>>>
>>>TIA
>>
>>depends on your definition of "parsing" ;-))
>>
>>luR = iif(vartype(m.lcS)=="C" and occurs("/",m.lcS), eval(m.lcS), m.lcS)
>
>How would you reverse that, Thomas?
>e.g. convert .25 to "1/4"

I did that once, but for inches only, and rounded it to eights. Something like
lparameter tnLen
lcInt=tran(int(tnLen), "999")
lnFrac=tnLen % 1
lnEights=round(lnFrac*8,0)
lcRet=ltrim(lcInt)+" "+tran(lnEights,"9")+"/8"
return lcRet
IIRC I also had some logic to see if lnEights was an even number, then it would be quarters, or if that was even, make it 1/2.

For the general case, I'd go for the divisors up to, say, 32, and then pick the one with the least rounding error.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Reply
Map
View

Click here to load this message in the networking platform