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
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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