Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate fraction from decimal
Message
From
09/03/2010 13:23:08
 
 
To
09/03/2010 08:32:47
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:
01453530
Views:
83
>Hello,
>
>Does somebody know how to calculate a fraction from a decimal?
>
>I have the user input 0.75 and the program should generate "3/4".
>
>I would highly appreciate your help.



I have assembled some pieces I had lying around
*_______________________________________________________________________________
function do_it()

	
	local numerator, denominator

	?NumToFraction(-.855, @m.numerator, @m.denominator),  m.numerator, m.denominator
		
	
	
endfunc
*_______________________________________________________________________________
function NumToFraction(n,  numerator, denominator)

	local fraction 
	
	if( m.n == 0 )
		fraction = '0'
		numerator = 0
		denominator = 1
	
	else
		denominator =   int(LowestCommonMultiple(m.n, 1) / abs(m.n))
		numerator  = int(m.n * m.denominator )
		fraction = transf(m.numerator) + '/' + transf(m.denominator)
	endif
	
	return m.fraction 
	
endfunc
*_______________________________________________________________________________
function GreatestCommonDivisor(x, y) 

	
	if( ( m.x == 0) or ( m.y == 0 ) )
		return 0
	endif
	
	x = abs(m.x)
	y = abs(m.y)
	
	do while ( true )
		x = mod(m.x, m.y)
		
		if( m.x == 0 )
			return m.y
		endif
			
		y = mod(m.y, m.x)
		
		if( m.y == 0 )
			return m.x
		endif
		
			
	enddo
endfunc
*_______________________________________________________________________________
function LowestCommonMultiple(x, y)
	
	local gcd
	
	gcd = GreatestCommonDivisor(m.x, m.y)
	
	if( m.gcd == 0 )
		error 1307
	endif
	
	return abs( int((m.x * m.y) / m.gcd ) )

endfunc
*_______________________________________________________________________________
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform