# [RUBY] How to get an arbitrary digit from a number of 2 or more digits! !!

Good evening! !! Aloha man, Yasunori: kissing_smiling_eyes:

I'll also write a reminder that it took me a while to understand while studying this week: smirk:

The theme of this time is as the title says ** "What should I do when I want to get the number of the desired digit from the number of 2 digits or more! ?? : sob: ”** Let's solve it! !!

By the way, all the calculations are integers, so I will omit it this time, but there are some changes such as using the to_i method if there is a decimal point: wink:

# How do you get the number of digits you want?

Then, first of all, I will write a lot about how to take the numerical value of ** up to the hundreds place **!

``````num = 123

#If you want 100 digits
digit_100 = (num / 100) % 10

#If you want 10 digits
digit_10 = (num / 10) % 10

#If you want a digit of 1
digit_1 = (num / 1) % 10

puts "The number of 100 digits is#{digit_100}is"
puts "The 10-digit number is#{digit_10}is"
puts "The one-digit number is#{digit_1}is"
``````

Now that you've written the code! !! If you do this, do you get the value of the digit you want ...: sweat:

## result

``````The number of digits of 100 is 1.
The 10 digit number is 2
The number of 1 digit is 3
``````

It was output properly! !! : grin:

Let's take a closer look.

## Commentary

Let's see the case of 10 digits

``````digit_10 = (num / 10) % 10
``````

It has become. First, calculate the first half of the formula and you will get ** 12 **. From here, the part that was originally ** 10 digits ** has now moved to ** 1 digits **. After that, I don't need a number with 10 or more digits, so I just want to calculate and only 1 digit ...: thinking:

Really! !! ** Should I divide by 10 and return the 1 digit as a remainder! !! ** That's why the second half of "%" comes into play: grin:

Because the remainder of dividing 12 by 10 is the output result

``````2
``````

Will be returned as a result! !!

In the first half of this, ** 10 **, this time it was 10 digits, so divide it by 10, but if it is ** 10,000 digits, it is 10,000 **, and if it is ** 1 million, it is 1 million **. If you divide by a digit, the number of the desired digit will be returned to the 1 digit! !!

# Why did you need time to understand?

If you look at this explanation, "Hmm? Isn't it surprisingly easy? Why did it take so long to understand? Some people may think ": thinking:".

Actually, the problem that was raised when I was thinking about how to get the number of digits this time was ** "Get each number from the two-digit number" **! !!

As I explained at the time of the explanation earlier, if you want a digit of 1, ** "You just have to use the remainder after dividing by 10 as the return value" **. And the 10 digits became ** "You can get the number of digits just by dividing by 10" **.

``````digit_10 = num / 10
digit_1 = num % 10
``````

I ended up with the code: joy: Even with this, if it is only 2 digits, the answer is the same, so "That ... why is this not good? 』It has become ...

With this code, when the number of digits increases, you can handle it by just getting the number of the 1st digit and the 1st digit, but you can not get the middle digit! !!

When I realized that, I could understand it immediately, but it took me a while to notice it: sweat_smile:

# Conclusion

Even if it looks so easy, I can't understand it at all if I'm addicted to it. However, the feeling of accomplishment when you understand ** is irreplaceable. I felt like I was already in the brain juice Dopadopa: laughing: