1. Getting the th of a Number

Anybody know if we have a library for getting the 'th' of a number. grin I know that's not the term... but I don't know what it is.

I want to input 15 and get back 15th, or input 3 and get back 3rd.

Thanks! smile

new topic     » topic index » view message » categorize

2. Re: Getting the th of a Number

euphoric said...

Anybody know if we have a library for getting the 'th' of a number. grin I know that's not the term... but I don't know what it is.

I want to input 15 and get back 15th, or input 3 and get back 3rd.

Thanks! smile

The word is ordinal. I haven't used it, but an archive search for ordinal turned up David Moneys Number conversions library.

Matt

new topic     » goto parent     » topic index » view message » categorize

3. Re: Getting the th of a Number

euphoric said...

Anybody know if we have a library for getting the 'th' of a number. grin I know that's not the term... but I don't know what it is.

I want to input 15 and get back 15th, or input 3 and get back 3rd.

Thanks! smile

function th(integer n) 
  if n = 1 then 
    return "1st" 
  elsif n = 2 then 
   return "2d" 
  elsif n = 3 then 
   return "3rd" 
  else 
   return sprintf("%dth",n) 
  end if 
end function 


jacques

new topic     » goto parent     » topic index » view message » categorize

4. Re: Getting the th of a Number

mattlewis said...
euphoric said...

I want to input 15 and get back 15th, or input 3 and get back 3rd.

The word is ordinal. I haven't used it, but an archive search for ordinal turned up David Moneys Number conversions library.

Ha! Thanks Matt. Good find! smile

new topic     » goto parent     » topic index » view message » categorize

5. Re: Getting the th of a Number

jacquesd said...
euphoric said...

I want to input 15 and get back 15th, or input 3 and get back 3rd.

function th(integer n) 
  if n = 1 then 
    return "1st" 
  elsif n = 2 then 
   return "2d" 
  elsif n = 3 then 
   return "3rd" 
  else 
   return sprintf("%dth",n) 
  end if 
end function 

What about 23? 78? etc... smile

new topic     » goto parent     » topic index » view message » categorize

6. Re: Getting the th of a Number

constant ordstr = "thstndrd" 
function ordinal(integer n) 
  sequence res 
  integer i 
 
  res = sprintf("%d", n) 
 
  i = (find(res[$], "123") + 1 * 2) - 1 
 
  res &= ordstr[ i .. i + 1] 
 
  return res 
end function 
new topic     » goto parent     » topic index » view message » categorize

7. Re: Getting the th of a Number

Oops! Correction ...

constant ordstr = "thstndrd" 
function ordinal(integer n) 
  sequence res 
  integer i 
 
  res = sprintf("%d", n) 
 
  i = (find(res[$], "123") + 1) * 2 - 1 
 
  res &= ordstr[ i .. i + 1] 
 
  return res 
end function 
new topic     » goto parent     » topic index » view message » categorize

8. Re: Getting the th of a Number

euphoric said...
jacquesd said...
euphoric said...

I want to input 15 and get back 15th, or input 3 and get back 3rd.

function th(integer n) 
  if n = 1 then 
    return "1st" 
  elsif n = 2 then 
   return "2d" 
  elsif n = 3 then 
   return "3rd" 
  else 
   return sprintf("%dth",n) 
  end if 
end function 

What about 23? 78? etc... smile

Well! it was a fast try. For sure David Moneys did it right.

regards, Jacques

new topic     » goto parent     » topic index » view message » categorize

9. Re: Getting the th of a Number

constant ORDINAL_SUFFIX = {"st","nd","rd","th"} 
function ordinal(integer val) 
 integer r 
  r = remainder(val,10) 
  if ((r < 1) or (r > 3) or (remainder(val,100) = 11)) then r = 4 end if 
  return ORDINAL_SUFFIX[r] 
end function 
new topic     » goto parent     » topic index » view message » categorize

10. Re: Getting the th of a Number

I didn't account for the rest of the 'teens

constant ORDINAL_SUFFIX = {"st","nd","rd","th"}  
function ordinal(integer val)  
 integer r  
  r = remainder(val,10)  
  if ((r < 1) or (r > 3) or (r = remainder(val,100)-10)) then r = 4 end if  
  return ORDINAL_SUFFIX[r]  
end function  
new topic     » goto parent     » topic index » view message » categorize

11. Re: Getting the th of a Number

Damn... I forgot about 11th, 12th, and 13th too.

constant ordstr = "thstndrd" 
function ordinal(integer n) 
  sequence res 
  integer i 
 
  res = sprintf("%d", n) 
 
  if n < 11 or n > 13 then 
     i = (find(res[$], "123") + 1) * 2 - 1 
  else 
     i = 1 
  end if 
 
  res &= ordstr[ i .. i + 1] 
 
  return res 
end function 
new topic     » goto parent     » topic index » view message » categorize

12. Re: Getting the th of a Number

Race results...

include std/console.e 
include std/text.e 
 
object VOID 
 
constant ORDINAL_SUFFIX = {"st","nd","rd","th"}   
function ordinal1(integer val)   
 integer r   
  r = remainder(val,10)   
  if ((r < 1) or (r > 3) or (r = remainder(val,100)-10)) then r = 4 end if   
  return sprintf("%d",val) & ORDINAL_SUFFIX[r]   
end function   
 
constant ordstr = "thstndrd"  
function ordinal2(integer n)  
  sequence res  
  integer i  
  
  res = sprintf("%d", n)  
  
  if n < 11 or n > 13 then  
     i = (find(res[$], "123") + 1) * 2 - 1  
  else  
     i = 1  
  end if  
  
  res &= ordstr[ i .. i + 1]  
  
  return res  
end function 
 
procedure main() 
integer y 
sequence x, test, res 
atom ti 
 
test = {"",""} 
res = {0,0} 
 
puts(1,"Testing ordinal() by scooby.\n") 
y = 0 
ti = time() + 3 
while time() <  ti do 
	x = ordinal1( y ) 
	y += 1 
end while 
printf(1,"Calls in 3 seconds: %d\n",{y}) 
puts(1,"Confirming accuracy:\n") 
for t=1 to 50 do 
	test[1] &= ordinal1(t) 
	puts(1,ordinal1(t) & " ") 
end for 
res[1] = y 
 
puts(1,"Testing ordinal() by Parnell.\n") 
y = 0 
ti = time() + 3 
while time() <  ti do 
	x = ordinal2( y ) 
	y += 1 
end while 
printf(1,"Calls in 3 seconds: %d\n",{y}) 
puts(1,"Confirming accuracy:\n") 
for t=1 to 50 do 
	test[2] &= ordinal2(t) 
	puts(1,ordinal2(t) & " ") 
end for 
res[2] = y 
 
if equal(test[1],test[2]) then 
	puts(1,"RESULTS ARE EQUAL.\n") 
else 
	puts(1,"RESULTS ARE NOT EQUAL.\n") 
end if 
 
if res[1] > res[2] then 
	puts(1,"Test 1 faster.") 
else 
	puts(1,"Test 2 faster.") 
end if 
 
VOID = wait_key() 
end procedure 
 
main() 
new topic     » goto parent     » topic index » view message » categorize

13. Re: Getting the th of a Number

Wow! Forgot to include David Money's ordinal()... Here it is with a slight modification. I don't know if it's faster, but it is 4.0. smile

include std/console.e  
include std/text.e  
  
object VOID  
  
constant ORDINAL_SUFFIX = {"st","nd","rd","th"}    
function ordinal1(integer val)    
 integer r    
  r = remainder(val,10)    
  if ((r < 1) or (r > 3) or (r = remainder(val,100)-10)) then r = 4 end if    
  return sprintf("%d",val) & ORDINAL_SUFFIX[r]    
end function    
  
constant ordstr = "thstndrd"   
function ordinal2(integer n)   
  sequence res   
  integer i   
   
  res = sprintf("%d", n)   
   
  if n < 11 or n > 13 then   
     i = (find(res[$], "123") + 1) * 2 - 1   
  else   
     i = 1   
  end if   
   
  res &= ordstr[ i .. i + 1]   
   
  return res   
end function  
 
global function ordinal3(integer num) 
--returns number with th, nd, st, etc. 
sequence ending 
integer tens,units 
	tens=remainder(num,100) --00 to 99 
	units=remainder(tens,10) --0 to 9 
	tens=floor(tens/10) -- 0 to 9 
	if tens=1 then 
		units=4 --11 through 13 use th 
	end if 
	 
	switch units do 
		case 1: 
			ending="st" 
			break 
		case 2: 
			ending="nd" 
			break 
		case 3: 
			ending="rd" 
			break 
		case else 
			ending="th" 
	end switch 
	 
return sprintf("%d",{num})&ending 
end function 
  
procedure main()  
integer y  
sequence x, test, res  
atom ti  
  
test = {"","",""}  
res = {0,0,0}  
  
puts(1,"Testing ordinal() by scooby.\n")  
y = 0  
ti = time() + 3  
while time() <  ti do  
	x = ordinal1( y )  
	y += 1  
end while  
printf(1,"Calls in 3 seconds: %d\n",{y})  
puts(1,"Confirming accuracy:\n")  
for t=1 to 50 do  
	test[1] &= ordinal1(t)  
	puts(1,ordinal1(t) & " ")  
end for  
res[1] = y  
  
puts(1,"Testing ordinal() by Parnell.\n")  
y = 0  
ti = time() + 3  
while time() <  ti do  
	x = ordinal2( y )  
	y += 1  
end while  
printf(1,"Calls in 3 seconds: %d\n",{y})  
puts(1,"Confirming accuracy:\n")  
for t=1 to 50 do  
	test[2] &= ordinal2(t)  
	puts(1,ordinal2(t) & " ")  
end for  
res[2] = y  
 
puts(1,"Testing ordinal() by Money.\n")  
y = 0  
ti = time() + 3  
while time() <  ti do  
	x = ordinal3( y )  
	y += 1  
end while  
printf(1,"Calls in 3 seconds: %d\n",{y})  
puts(1,"Confirming accuracy:\n")  
for t=1 to 50 do  
	test[3] &= ordinal2(t)  
	puts(1,ordinal3(t) & " ")  
end for  
res[3] = y  
  
if equal(test[1],test[2]) and equal(test[1],test[3]) then  
	puts(1,"RESULTS ARE EQUAL.\n")  
else  
	puts(1,"RESULTS ARE NOT EQUAL.\n")  
end if  
  
if res[1] > res[2] then  
	if res[1] > res[3] then 
		puts(1,"Test 1 fastest.")  
	else 
		puts(1,"Test 3 fastest.") 
	end if 
else  
	if res[2] > res[3] then 
		puts(1,"Test 2 fastest.") 
	else 
		puts(1,"Test 3 fastest.") 
	end if 
end if  
  
VOID = wait_key()  
end procedure  
  
main() 
new topic     » goto parent     » topic index » view message » categorize

14. Re: Getting the th of a Number

There's a bug in the latest test

for t=1 to 50 do 
	test[3] &= ordinal2(t)   
	puts(1,ordinal3(t) & " ")   
end for   
res[3] = y 

Nice thread :)

new topic     » goto parent     » topic index » view message » categorize

15. Re: Getting the th of a Number

I forgot to show the error (in case somebody doesn't note it)

for t=1 to 50 do  
	test[3] &= ordinal3(t) -- This was ordinal2(t) 
	puts(1,ordinal3(t) & " ")    
end for    
res[3] = y  

By the way, I found a creole (I think) bug, if the latest sentence of your post is </eucode> (with an opening <eucode> before) it will break formatting next to it. I can test this in the preview mode. Just enter: "<eucode></eucode>"

new topic     » goto parent     » topic index » view message » categorize

16. Re: Getting the th of a Number

gbonvehi said...

There's a bug in the latest test

for t=1 to 50 do 
	test[3] &= ordinal2(t)   
	puts(1,ordinal3(t) & " ")   
end for   
res[3] = y 

Nice thread :)

Thanks for the bug report! grin

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu