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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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