Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate fraction from decimal
Message
From
13/03/2010 10:42:04
 
 
To
09/03/2010 16:58:01
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2008 Server
Database:
MySQL
Application:
Desktop
Miscellaneous
Thread ID:
01453419
Message ID:
01454300
Views:
55
>>It would if you had
>>
>>#DEFINE true .T.
>>
>>>
>>>Absolute 1st class! Change true to .T. as Fox did not like this, but again: very good.
>>>Thanks for sharing with us.
>>>
>Sergey, of course, but that was not listed in the code.
>
> An other thing: I am less enthousiastic after some tests:
>
>
>0.33 results in 33/100
>0.333333333333333 results in 0/3
>0.3333333333333333 results in 1/3
>0.6666 results in 1/3
>
>I take it this has something to do with how VFP rounds
>
>Regards,
>
>Koen
local numerator, denominator

? Num2Fraction(-.3333333333333333, @m.numerator, @m.denominator)


FUNCTION Num2Fraction (x,  numerator AS @, denominator AS @)
	IF EMPTY(m.x)
		STORE 0 TO numerator
		STORE 1 TO denominator
		RETURN '0'
	ENDIF
	STORE 10^MAX(LEN(RTRIM(CAST(m.x % 1 AS M),"0"))-2,0)	TO denominator
	PRIVATE n
	STORE GCD(m.denominator, ROUND(m.x * m.denominator,0))	TO n
	STORE m.denominator/m.n					TO denominator
	STORE ROUND(m.x * m.denominator,0)			TO numerator
	RETURN TRANSFORM(m.numerator) + '/' + transf(m.denominator)
ENDFUNC

FUNCTION GCD(i1,i2)
	DO WHILE m.i2 # 0
		STORE m.i1 % m.i2	TO i1
		IF EMPTY(m.i1)
			RETURN ABS(m.i2)
		ENDIF
		STORE m.i2%m.i1	TO i2
	ENDDO
	RETURN IIF(ISNULL(m.i2),DAY(NULL),ABS(m.i1))
ENDFUNC

FUNCTION LCM (i1,i2)
	RETURN ABS( ROUND(m.i1 * m.i2,0) / EVL(GCD(m.i1, m.i2),NULL))
ENDFUNC
Previous
Reply
Map
View

Click here to load this message in the networking platform