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:
The Code:
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.
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:
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'd like in their place
private constant string AND = "and "
private constant string SPACE = " "
private constant string HYPHEN = "-"
private constant string NEGATIVE = "Negative"
private constant string COMMA = ","
// 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>0) then
set r = OnesTeens[hundreds] + SPACE + Denom[0]+SPACE+AND
endif
if (tens>0) then
if (tens<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>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 = ""
local string temp = ""
local string neg = ""
local integer denom = 0
local integer n = 0
if (i<0) then
set i = -i
set neg = NEGATIVE+SPACE
endif
if (i<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>0) then
set r = Denom[denom]+COMMA+SPACE+r
endif
set r = temp+SPACE+r
endif
set denom = denom+1
exitwhen (i<=0)
endloop
return neg+r
endfunction
private function init takes nothing returns nothing
set OnesTeens[0] = "Zero"
set OnesTeens[1] = "One"
set OnesTeens[2] = "Two"
set OnesTeens[3] = "Three"
set OnesTeens[4] = "Four"
set OnesTeens[5] = "Five"
set OnesTeens[6] = "Six"
set OnesTeens[7] = "Seven"
set OnesTeens[8] = "Eight"
set OnesTeens[9] = "Nine"
set OnesTeens[10] = "Ten"
set OnesTeens[11] = "Eleven"
set OnesTeens[12] = "Twelve"
set OnesTeens[13] = "Thirteen"
set OnesTeens[14] = "Fourteen"
set OnesTeens[15] = "Fifteen"
set OnesTeens[16] = "Sixteen"
set OnesTeens[17] = "Seventeen"
set OnesTeens[18] = "Eighteen"
set OnesTeens[19] = "Nineteen"
set Tens[0] = "Twenty"
set Tens[1] = "Thirty"
set Tens[2] = "Forty"
set Tens[3] = "Fifty"
set Tens[4] = "Sixty"
set Tens[5] = "Seventy"
set Tens[6] = "Eighty"
set Tens[7] = "Ninety"
set Denom[0] = "Hundred"
set Denom[1] = "Thousand"
set Denom[2] = "Million"
set Denom[3] = "Billion"
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.