Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sensible fractions from percentages

Status
Not open for further replies.

GriffMG

Programmer
Mar 4, 2002
6,334
FR
Today, for the first time since I started programming - about 30 years, a client asked for a percentage to be shown as a fraction.

This is for 'strike rates' for their estimators - if they achieve a 20% success rate, they want to see 1/5 (one in five)

I can't find any code for this - so I am using this:

Code:
FUNCTION PC2FRACT
	PARAMETER m.PERVALUE
	PRIVATE m.PERVALUE,m.MYDIVIDER,m.FLAG,I,m.STRING
	m.MYDIVIDER = 100
	m.STRING = ""
	m.PERVALUE = (ROUND(m.PERVALUE*4,0) /4 )
	m.FLAG = .F.
	I = m.MYDIVIDER
	** run down from 100 to highest common divider preserving index
	DO WHILE m.FLAG = .F. .AND. I >= 1
		IF INT(m.MYDIVIDER/I) = m.MYDIVIDER /I .AND. INT(m.PERVALUE /I) = m.PERVALUE /I
			** bingo
			m.FLAG = .T.
		ELSE
			** come down in units of 25 initially
			IF I > 25
				I = I - 25
			ELSE
				** then 5s
				IF I > 5
					I = I - 5
				ELSE
					** and then 1s
					I = I -1
				ENDIF
			ENDIF

		ENDIF
	ENDDO
	IF m.FLAG
		** if you got a sensible match, use it
		m.STRING = STR(m.PERVALUE / I,4,0) +" / "+ STR(m.MYDIVIDER / I,4,0)
	ELSE
		** other express as over 100
		m.STRING = STR(m.PERVALUE ,4,0) + " / 100"
	ENDIF

	RETURN(m.STRING)

Does anyone have any other ideas?


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Just the range of exactly 0..100%? No fractions of percentages (they would result in complex fractions anyway)?
Why not prepare an array for that? 101 elements. Or a lookup table, of course, which could also include some simple fractions with infinite rational decimals like 1/3, 1/6, etc.

Bye, Olaf.



 
Hi Olaf

A lookup table would work, for the integer values 0-100, the real world values cannot exceed those bounds (you can't win 110% or less than nothing)

I thought that values in the two decimal places region do occur and sometimes through up 'sensible' fractions - but I can't find any now!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Well, actually you can win 110%, meaning more than doubling the stake.
At least a table can be populated by hand so you don't need to think about code and you may use SEEK with SET NEAR ON to find near matches.

You do have sensible fractions, don't you?
5% = 1/20, 10% = 1/10, 11% ca 1/9, 40% = 2/5 etc.
You just need to exercise shortening n/100, sometimes maybe not too narrow(?) If only exact fractions are wanted the prime factors of 100 are 2*2*5*5 so only n divisible by 2 and 5 have exact fractions.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top