1. Getting the th of a Number
- Posted by euphoric (admin) Oct 15, 2008
- 1145 views
- Last edited Oct 16, 2008
Anybody know if we have a library for getting the 'th' of a number. 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!
2. Re: Getting the th of a Number
- Posted by mattlewis (admin) Oct 15, 2008
- 1192 views
- Last edited Oct 16, 2008
Anybody know if we have a library for getting the 'th' of a number. 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!
The word is ordinal. I haven't used it, but an archive search for ordinal turned up David Moneys Number conversions library.
Matt
3. Re: Getting the th of a Number
- Posted by jacquesd Oct 15, 2008
- 1113 views
- Last edited Oct 16, 2008
Anybody know if we have a library for getting the 'th' of a number. 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!
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
4. Re: Getting the th of a Number
- Posted by euphoric (admin) Oct 15, 2008
- 1154 views
- Last edited Oct 16, 2008
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!
5. Re: Getting the th of a Number
- Posted by euphoric (admin) Oct 15, 2008
- 1133 views
- Last edited Oct 16, 2008
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...
6. Re: Getting the th of a Number
- Posted by DerekParnell (admin) Oct 15, 2008
- 1124 views
- Last edited Oct 16, 2008
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
7. Re: Getting the th of a Number
- Posted by DerekParnell (admin) Oct 15, 2008
- 1112 views
- Last edited Oct 16, 2008
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
8. Re: Getting the th of a Number
- Posted by jacquesd Oct 15, 2008
- 1114 views
- Last edited Oct 16, 2008
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...
Well! it was a fast try. For sure David Moneys did it right.
regards, Jacques
9. Re: Getting the th of a Number
- Posted by scooby Oct 16, 2008
- 1129 views
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
10. Re: Getting the th of a Number
- Posted by scooby Oct 16, 2008
- 1145 views
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
11. Re: Getting the th of a Number
- Posted by DerekParnell (admin) Oct 16, 2008
- 1088 views
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
12. Re: Getting the th of a Number
- Posted by euphoric (admin) Oct 16, 2008
- 1137 views
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()
13. Re: Getting the th of a Number
- Posted by euphoric (admin) Oct 16, 2008
- 1089 views
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.
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()
14. Re: Getting the th of a Number
- Posted by gbonvehi Oct 16, 2008
- 1056 views
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 :)
15. Re: Getting the th of a Number
- Posted by gbonvehi Oct 16, 2008
- 1039 views
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>"
16. Re: Getting the th of a Number
- Posted by euphoric (admin) Oct 16, 2008
- 1065 views
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!