How to slice a number into separate digits
A simple question people asked me several times. "I need to use pictures to display a number, how can I get the all the digits?"
Well I'll tell you how. It's grade 10 maths, but hey we can all forget sometimes.The simple way
This method is the easiest, but has a big drawback: you'll get the digits reversed. For this tutorial purpose, let's consider the number 256. You have three pictures, each one representing a figure. So how can you separate the digits of 256?
Divide per 10. You'll get 25,6. See what happens there? You just put a comma in the number, leaving the 6 all alone. It means that 6 is the remainder or the operation 256/10. In other words, 6 is the result of the modulus operation 256 % 10
Modulus can be done with the variable operation command if you intend to event your system. Its symbol is either %
Now that you got the last digit, time to retrieve other ones. You probably understood that if order to get the 5, you need to transform your 256 into 25. Simply divide your number by 10 and floor the result. In most cases RPG Maker do that automatically as long as you aren't using float numbers.
You can then perform another modulus operation to get the 5, and for the 2, I believe you know what to do now.The clean way
This method is the most efficient and involves the use of base 10 logarithm. So what is a logarithm? A mathematical function which has for result the exponent to which you have to raise the function base in order to produce a given number. Sounds complicated, but it's in fact pretty simple.
For instance, you want to know the base 10 logarithm of 100. In other words, how many times do you need to multiply 10 by itself to produce 100? Obviously 2 times.
Let's use again the number 256, and see how we can get the digits in the right order with this method.
Create a new variable, independent of the one used to store the whole number. Set it to the log10 of the number.
number = 256
digit_number = Math.log10(number).floor
Computing logarithms manually can be tedious, so if you are eventing you picture display system, use a Call Script command.
$game_variables[variable_id] = Math.log10($game_variables[first_variable_id]).floor
This variable currently stores 2, but that is not the first digit of 256! It represents how many digits the number has, minus 1. Simply put, you know that your number has 3 digits.
Now, you need to extract the digits and display them. Since you don't want to create a set of variables for each digit (which would require you to use conditions to check the total number), you can simply create a loop to re-use everything.Ruby version:
current_digit = 0Event version:
while digit_number >= 0
current_digit = number / (10 ** digit_number)
# here you display the first picture however you intended to do.
number %= 10 ** digit_number
digit_number -= 1
So what did we do in this loop? Several things.
- Extracted the digit. In order to get 2 in 256, you need to divide 256 by 100, correct? Well, that's exactly that. This line, current_digit = number / (10 ** digit_number), divides the original number by 10 multiplied by itself as many times as there are digits left after. In this case, 2 times.
- After you displayed your picture, you do exactly the same as above but with a modulus operation to get rid of the 2 and keep the 56.
- Since you got one digit already, you subtract 1 from the total, and the loop restarts until no digit is left.
So that's it. I hope it wasn't too cryptic so feel free to ask for questions if you didn't understand.