Snippet Int2WordString

quraji

zap
Reaction score
144
What is it?
It is a function that will convert any integer (that can exist in WC3) into word form. If that's not clear, I mean that it will convert, say, 10352 in to "Ten Thousand, Three Hundred and Fifty-Two".

Neat, how do I use it?
Just call Int2WordString(n) where n is the integer you wish to convert. It will return a string in the above specified form. The only configuration available is optional, and that is to change the punctuation, "and", and "Negative" to what you like.

Picture?
Yep:
u6lvr.jpg


The Code:
JASS:
library Int2WordString initializer init
    // by quraji
    // Converts any integer to word form (163 -> One Hundred and Sixty-Three)
    // This was in shambles in an old directory, from when I first wanted to make it
    // I decided to revive it <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />
    // Have fun!
    
globals
    // CONFIG
    // change these to what you&#039;d like in their place
    private constant string AND = &quot;and &quot;
    private constant string SPACE = &quot; &quot;
    private constant string HYPHEN = &quot;-&quot;
    private constant string NEGATIVE = &quot;Negative&quot;
    private constant string COMMA = &quot;,&quot;
    // ENDCONFIG
    
    private string array OnesTeens [20]
    private string array Tens [8]
    private string array Denom [4]
endglobals

 // converts numbers less than a thousand to words
private function Int2WordString_helper takes integer i returns string
    local integer hundreds = i/100
    local integer tens = ModuloInteger(i, 100)
    local integer ones = 0
    local string r = null
    
    if (hundreds&gt;0) then
        set r = OnesTeens[hundreds] + SPACE + Denom[0]+SPACE+AND
    endif
    if (tens&gt;0) then
        if (tens&lt;20) then
            if (r==null) then
                set r = OnesTeens[tens]
            else
                set r = r+SPACE+OnesTeens[tens]
            endif
        else
            set ones = ModuloInteger(tens, 10)
            set tens = tens/10
            if (r==null) then
                set r = Tens[tens-2]
            else
                set r = r+Tens[tens-2]
            endif
            if (ones&gt;0) then
                set r = r+HYPHEN+OnesTeens[ones]
            endif
        endif
    endif
    
    return r    
endfunction

 // works by reading right-to-left
 // each step in the loop converts the number into the next biggest denomination
 // then, converting that to a number (which can only be as high as 999) to words, and appending the appropriate denomination
function Int2WordString takes integer i returns string
    local string r = &quot;&quot;
    local string temp = &quot;&quot;
    local string neg = &quot;&quot;
    local integer denom = 0
    local integer n = 0
    
    if (i&lt;0) then
        set i = -i
        set neg = NEGATIVE+SPACE
    endif
    if (i&lt;20) then
        return neg+OnesTeens<i>
    endif
    
    loop
        set n = ModuloInteger(i, 1000)
        set i = i/1000
        
        set temp = Int2WordString_helper(n)
        if (temp!=null) then
            if (denom&gt;0) then
                set r = Denom[denom]+COMMA+SPACE+r
            endif
            set r = temp+SPACE+r
        endif
        
        set denom = denom+1
        exitwhen (i&lt;=0)
    endloop
    
    return neg+r
endfunction

private function init takes nothing returns nothing
    set OnesTeens[0] = &quot;Zero&quot;
    set OnesTeens[1] = &quot;One&quot;
    set OnesTeens[2] = &quot;Two&quot;
    set OnesTeens[3] = &quot;Three&quot;
    set OnesTeens[4] = &quot;Four&quot;
    set OnesTeens[5] = &quot;Five&quot;
    set OnesTeens[6] = &quot;Six&quot;
    set OnesTeens[7] = &quot;Seven&quot;
    set OnesTeens[8] = &quot;Eight&quot;
    set OnesTeens[9] = &quot;Nine&quot;
    set OnesTeens[10] = &quot;Ten&quot;
    set OnesTeens[11] = &quot;Eleven&quot;
    set OnesTeens[12] = &quot;Twelve&quot;
    set OnesTeens[13] = &quot;Thirteen&quot;
    set OnesTeens[14] = &quot;Fourteen&quot;
    set OnesTeens[15] = &quot;Fifteen&quot;
    set OnesTeens[16] = &quot;Sixteen&quot;
    set OnesTeens[17] = &quot;Seventeen&quot;
    set OnesTeens[18] = &quot;Eighteen&quot;
    set OnesTeens[19] = &quot;Nineteen&quot;
    
    set Tens[0] = &quot;Twenty&quot;
    set Tens[1] = &quot;Thirty&quot;
    set Tens[2] = &quot;Forty&quot;
    set Tens[3] = &quot;Fifty&quot;
    set Tens[4] = &quot;Sixty&quot;
    set Tens[5] = &quot;Seventy&quot;
    set Tens[6] = &quot;Eighty&quot;
    set Tens[7] = &quot;Ninety&quot;
    
    set Denom[0] = &quot;Hundred&quot;
    set Denom[1] = &quot;Thousand&quot;
    set Denom[2] = &quot;Million&quot;
    set Denom[3] = &quot;Billion&quot;
endfunction
endlibrary
</i>


I tried the number 961238162919151 and it didn't work, this sucks!
Sorry, integers in WC3 only go from -2,147,483,647 to 2,147,483,647. I wish I could do bigger numbers too =[

That's it, thanks.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Why not shorten it to Int2String... Even Int2Word.
 

quraji

zap
Reaction score
144
>>Int2String
Because, that makes it sound like it does the same thing as I2S.

>>Int2Word
Because, it will return multiple words. Int2WordString sounds best to me, as it implies that you may have a "string" of "words" from the integer.

Thanks for the comment =]
 

Azlier

Old World Ghost
Reaction score
461
At last, someone finished what I could not. Can't work on WC3 anymore, :(. Only every other weekend.

Though, I hate the capital letters. (Ten Thousand, Three Hundred and Fifty-Two) It makes it unusable in the middle of sentences.

Also, "three hundred and forty" is not proper English. The word "and" does not belong there. It's "three hundred forty".
 

quraji

zap
Reaction score
144
>>Though, I hate the capital letters. (Ten Thousand, Three Hundred and Fifty-Two) It makes it unusable in the middle of sentences.

I don't like them either, but it's very easy to do StringCase(Int2WordString(1234), false). Much easier than capitalizing every word from the output by hand, if you wanted it so.

>>Also, "three hundred and forty" is not proper English. The word "and" does not belong there. It's "three hundred forty".

Yes, I know that. That's why there's a configuration global to remove it :D (I figured it's easier to understand the config if the "and" is default)

Thanks for the comment =]
 

Azlier

Old World Ghost
Reaction score
461
>That's why there's a configuration global to remove it

And have two spaces instead of one? :confused:
 

quraji

zap
Reaction score
144
>That's why there's a configuration global to remove it

And have two spaces instead of one? :confused:

Ah. Before I had the constant as " and" but I figured that it was more user friendly to remove the space and code it in myself. I'll revert that. Thanks for pointing it out.
 

Dr.Jack

That's Cap'n to you!
Reaction score
109
Thats not needed, it's only a snippet with 1 public function.

JASS:
call BJDebugMsg(Int2WordString(69))

Usually posting map is a good idea. It helps people get full understanding and offers easier testing.
That's not necessary though if he chooses not to.

Edit
JASS:
    if (i&lt;20) then
    return OnesTeens<i>
endif
</i>


Should be

JASS:
    if (i&lt;20) then
    return neg+OnesTeens<i>
endif
</i>


No?
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top