Numbers To Words

A softcoded version of spellnum(), a function introduced to PennMUSH 1.7.3 that when given a number, return its written-out representation in words.

Author: Raevnos
Category: Functions
Commands: @create, @function, @lset, @set.
Compatibility: CobraMUSH, PennMUSH.

Instructions

Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Numbers To Words

@create Numbers to Words
@lock/Basic Numbers to Words==me
@lset Numbers to Words/Basic=no_inherit
@set Numbers to Words = VISUAL
&BREAK Numbers to Words=[iter(revwords(u(break_help,reverse(%0))),reverse(##))]
@set Numbers to Words/BREAK=visual
&BREAK_HELP Numbers to Words=[iter(foreach(split,%0),switch(mod(#@,3),0,##%b,##), ,)]
&DESCRIBE Numbers to Words=Contains the code for the @function N2A. Use u([num(me)]/number_fun,<NUMBER>) if you prefer. Another visual ufun(), u([num(me)]/break,<NUMBER>), will split a number into groups of 3. 1234 becomes 1 234, and so on.%r%rUse spellnum() in 1.7.3.
@set Numbers to Words/DESCRIBE=no_command visual public nearby
&NUMBER Numbers to Words=[setq(1, v(numbers_small))][setq(2, v(numbers_medium))][setq(3, v(numbers_big))][if( lt(%0, 0), negative [setq(0, u(break, delete(%0, 0, 1)))], setq(0, u(break, %0)))][setq(4, words(%q0))][switch(val(%0), 0, zero, squish(iter(%q0, u(number_3, val(##)) [switch(0, val(##),, eq(%q4,#@), extract(%q3, sub(%q4, #@), 1))])))]
&NUMBERS_BIG Numbers to Words=thousand million billion
&NUMBERS_MEDIUM Numbers to Words=ten twenty thirty fourty fifty sixty seventy eighty ninety
&NUMBERS_SMALL Numbers to Words=one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
&NUMBER_2 Numbers to Words=[switch(1,eq(%0,0),,lt(%0,20),extract(%q1,%0,1),[extract(%q2,left(%0,1),1)] [extract(%q1,right(%0,1),1)])]
&NUMBER_3 Numbers to Words=[switch(1,eq(%0,0),,eq(strlen(%0),3),[extract(%q1,left(%0,1),1)] hundred [u(number_2,right(%0,2))],u(number_2,%0))]
&NUMBER_FUN Numbers to Words=[switch(isnum(%0),0,#-1 FUNCTION (N2A) EXPECTS A NUMBER,ulocal(number,%0))]
@set Numbers to Words/NUMBER_FUN=visual
&SPLIT Numbers to Words=%0%b

look Numbers to Words