|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Tue Jan 08, 2008 5:04 am
WordToNum(): Converting a word number (twenty-three) to a numeric number (23) |
Quote: |
#VARIABLE Ones {one|two|three|four|five|six|seven|eight|nine}
#VARIABLE Tens {twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety}
#FUNCTION WordToNum {%case( %numwords( %replace( "%1", "-", " ")), %case( %ismember( "%1", "one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety"), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90), %eval( %case( %ismember( %word( %replace( "%1", "-", " "), 1), @tens), 20, 30, 40, 50, 60, 70, 80, 90) + %case( %ismember( %word( %replace( "%1", "-", " "), 2), @ones), 1, 2, 3, 4, 5, 6, 7, 8, 9)), %eval( %if( %pos( "thousand", "%1"), %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000), %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 100, 200, 300, 400, 500, 600, 700, 800, 900)) + %case( %ismember( %word( %replace( "%1", "-", " "), 3), "one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety"), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90)), %eval( %if( %pos( "thousand", "%1"), %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000), %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 100, 200, 300, 400, 500, 600, 700, 800, 900)) + %case( %ismember( %word( %replace( "%1", "-", " "), 3), @tens), 20, 30, 40, 50, 60, 70, 80, 90) + %case( %ismember( %word( %replace( "%1", "-", " "), 4), @ones), 1, 2, 3, 4, 5, 6, 7, 8, 9)), %eval( %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000) + %case( %ismember( %word( %replace( "%1", "-", " "), 3), @ones), 100, 200, 300, 400, 500, 600, 700, 800, 900) + %case( %ismember( %word( %replace( "%1", "-", " "), 5), "one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety"), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90)), %eval( %case( %ismember( %word( %replace( "%1", "-", " "), 1), @ones), 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000) + %case( %ismember( %word( %replace( "%1", "-", " "), 3), @ones), 100, 200, 300, 400, 500, 600, 700, 800, 900) + %case( %ismember( %word( %replace( "%1", "-", " "), 5), @tens), 20, 30, 40, 50, 60, 70, 80, 90) + %case( %ismember( %word( %replace( "%1", "-", " "), 6), "one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety"), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90)))}
|
This function will convert up to a 6-word word number into its numeric equivalent. Will produce numbers from 1 to 9999. The hyphen is optional, function will operate correctly with the twenty-one and twenty one formats.
Usage: @WordToNum(nine thousand nine hundred ninety-nine) |
|
_________________ EDIT: I didn't like my old signature |
|
|
|
Guinn Wizard
Joined: 03 Mar 2001 Posts: 1127 Location: London
|
Posted: Tue Jan 08, 2008 9:16 am |
Nifty, but wouldn't this be best suited to being recursive?
So @wordToNum("nine thousand nine hundred ninety nine")
=9000 + @wordToNum("nine hundred ninety nine")
=9900 + @wordToNum("ninety nine")
=9990 + @wordToNum("nine")
=9999
I'll have a go at a recursive one later in the week :) |
|
_________________ CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;) |
|
|
|
Leto GURU
Joined: 27 Sep 2000 Posts: 90 Location: USA
|
Posted: Tue Jan 08, 2008 3:38 pm |
Wow...nice. I did one of these on the old forums. I wonder if I can find it on the way back machine.
|
|
|
|
Leto GURU
Joined: 27 Sep 2000 Posts: 90 Location: USA
|
Posted: Fri Jan 11, 2008 5:17 am |
Update
I've uploaded a cMud version of my take on this problem to the package library.
I noticed Vijilante has one in his toolbox as well. From my testing his is a little faster, but I think mine is more elegant.
If cMud had inline functions (wishlist) I could make it faster. But as it stands it is a good example of recursion. It's for you Guinn. |
|
|
|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Sun Jan 13, 2008 4:24 am |
Quote: |
#FUNCTION NumToWord {%concat( %if( %len( %1) = 4, %concat( %item( @Ones, %left( %rightback( %1, 4), 1)), " thousand ")), %if( (%len( %1) >= 3) and (%left( %rightback( %1, 3), 1) != 0), %concat( %item( @Ones, %left( %rightback( %1, 3), 1)), " hundred ")), %if( %number( %rightback( %1, 2)) > 9, %if( %number( %rightback( %1, 2)) > 19, %concat( %item( "ten|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety", %left( %rightback( %1, 2), 1)), %if( %rightback( %1, 1), "-"), %item( @Ones, %rightback( %1, 1))), %case( %ismember( %rightback( %1, 2), "10|11|12|13|14|15|16|17|18|19"), ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen)), %item( @Ones, %rightback( %1, 1))))}
|
Here's the reverse of the function I posted. Has the same range of numbers (1-9999), and works by reading the number in three steps--thousands place, hundreds place, and then the last two digits as one entity.
NOTE: for some reason I haven't yet puzzled out, this function doesn't seem to work in a loop. |
|
_________________ EDIT: I didn't like my old signature |
|
|
|
Ghedemonas Novice
Joined: 15 Aug 2007 Posts: 39
|
|
|
|
Ranger_Aja Beginner
Joined: 22 Apr 2001 Posts: 15 Location: USA
|
Posted: Thu May 22, 2008 4:20 am |
This script is great and thanks for sharing!
BUT, I'm having trouble getting it to work when the line is an even hundred (one hundred, two hundred...etc). Any advice on how to solve this?
And a little tip for folks playing on muds that add an 'and' to the number text: Nesting a %remove function clears that up...@WordToNum(%remove(and,one hundred and one)) |
|
|
|
Ghedemonas Novice
Joined: 15 Aug 2007 Posts: 39
|
Posted: Fri Jun 20, 2008 10:46 pm |
CMUD Update see Main thread
|
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|