System Digit Checker + Number of Digits Checker

elmstfreddie

The Finglonger
Reaction score
203
Something I use in the digit checker is a loop that checks how many digits are in a number.
JASS:
local integer OriginalNumber = Whatever You Want
local integer NumOfDigits
local integer Index
local boolean FoundDigits
loop
    exitwhen (FoundDigits == true)
    if (Pow(10,Index) > OriginalNumber then
        set NumOfDigits = Index
        set FoundDigits = true
    endif
    set Index = Index + 1
endloop


This will find the individual digits in a number (integer only at the time, but I guess the same method would work for reals too, but the loop would have to go into negatives)

JASS:
function FindDigits takes nothing returns nothing
    local integer OriginalNumber = 28929163 //Random number for you to test!
    local integer NumOfDigits = 1
    local integer array Digits
    local integer array Index
    local boolean FoundDigits = false
    set Index[1] = 1
    set Index[3] = 1
    loop
        exitwhen (FoundDigits == true)
        if (Pow(10,Index[1])) > OriginalNumber then
            set NumOfDigits = Index[1]
            set FoundDigits = true
        endif
        set Index[1] = Index[1] + 1
    endloop
    set Index[2] = NumOfDigits
    loop
        exitwhen Index[2] == 0
        set Digits[Index[2]] = (R2I(OriginalNumber / (Pow(10,(Index[2] - 1)))))
        set OriginalNumber = (OriginalNumber - (Digits[Index[2]] * R2I(Pow(10,((Index[2] - 1))))))
        set Index[2] = Index[2] - 1
    endloop
    loop //This loop is unnecessary, it only displays the digits 1 by 1 so you can see it works <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />
        exitwhen Index[3] &gt; NumOfDigits
        call DisplayTextToPlayer(Player(0),0.00,0.00,(&quot;Digit #&quot; + I2S(Index[3]) + &quot; is &quot; + (I2S(Digits[Index[3]]))))
        set Index[3] = Index[3] + 1
    endloop
endfunction

OriginalNumber will be lost at the end since you subtract things, but this can easily be fixed by either having 2 separate variables (one for original, one for editing and getting digits from)
To retrieve the digits, just add them together after multiplying them by 10^([index] - 1), as seen when subtracting the digit from OriginalNumber.

This could be useful for scattering digits in a number throughout a save/load, then you can find them, multiply them by the power, and adding them all together.
 

Sooda

Diversity enchants
Reaction score
318
> StringLength(I2S(<number>)),
SubString(I2S(<number>),i,i + 1).

Can we say here: "omfg, Ace did it again !" ?

Just a cosmetic thing:
JASS:
local integer OriginalNumber = Whatever You Want
local integer NumOfDigits
local integer Index
local boolean FoundDigits
loop
    exitwhen (FoundDigits == true)
    if (Pow(10,Index) &gt; OriginalNumber then
        set NumOfDigits = Index
        set FoundDigits = true
    endif
    set Index = Index + 1
endloop

can be:
JASS:
local integer OriginalNumber = Whatever You Want
local integer NumOfDigits
local integer Index
local boolean FoundDigits
loop
    if (Pow(10,Index) &gt; OriginalNumber then
        set NumOfDigits = Index
        exitwhen true
    endif
    set Index = Index + 1
endloop

Your code is useful but Ace knows so much.
 

elmstfreddie

The Finglonger
Reaction score
203
Digit checker wasn't the important part however, I just ran into a problem and realized I needed to know the amount of digits.
And as par usual I thought way too much.
As for the exitwhen true thing, I never really knew I could do that. I guess it would be the same as putting exitwhen after then and doing exitwhen 1 == 1 lol
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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