[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" **.

How about that

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:

Let's try without giving up even if everyone encounters difficulties! !!

Recommended Posts

How to get an arbitrary digit from a number of 2 or more digits! !!
How to start a subscript from an arbitrary number in Ruby iterative processing
How to get a heapdump from a Docker container
History of a certain in-house SE
A simple example of an MVC model
[Java] How to convert one element of a String type array to an Int type
How to deploy a system created with Java (Wicket-Spring boot) to an on-campus server
How to get an arbitrary digit from a number of 2 or more digits! !!
[Swift] How to get the number of elements in an array (super basic)
An unsupported Java version How to get rid of errors
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to change a string in an array to a number in Ruby
How to get the longest information from Twitter as of 12/12/2016
How to Burning a Install Disk of Windows from Ubuntu
Let's get an internal representation of a floating point number
[jsoup] How to get the full amount of a document
[Swift5] How to get an array and the complement of arrays
[chown] How to change the owner of a file or directory
How to get the length of an audio file in java
[Swift] Summary of how to remove elements from an array (personal note)
How to develop from VScode in a remote destination environment or a remote destination container environment
[IOS] How to get data from DynamoDB
Try to issue or get a card from Jave to Trello using API
Extract each digit number from a 3-digit integer
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Introduction] A collection of carefully selected links from how to get started on GitHub to how to solve errors when you encounter an error on Git.
[Java] How to convert one element of a String type array to an Int type
graphql-ruby: How to get the name of query or mutation in controller Note
How to get the ID of a user authenticated with Firebase in Swift
[Spring Boot] How to get properties dynamically from a string contained in a URL
[Rails] How to get rid of flash messages in a certain amount of time
How to make a judgment method to search for an arbitrary character in an array
How to get Class from Element in Java
How to get today's day of the week
[Java] Get a random value from an array
How to get SIMD optimization from HotSpot JavaVM
[Java] How to get the authority of the folder
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
I want to output the character number from the left where an arbitrary character string appears.
What to do if you get a wrong number of arguments error in binding.pry
A memo about the types of Java O/R mappers and how to select them
[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)
[Java] How to get the URL of the transition source
How to use an array for a TreeMap key
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
[Java] How to get a request by HTTP communication
How to output standard from an array with forEach
[Android] How to get the setting language of the terminal
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Rails] How to get the contents of strong parameters
Send a command to Spigot from an external process
[Swift] How to get the document ID of Firebase
How to get started with creating a Rails app
How to get jdk etc from oracle with cli
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
[Technical memo] Things to be careful of from an engineer's point of view when creating a view
The story of migrating a stray batch without an owner from EC2 to a Docker environment
Story from inexperienced x self-study to becoming an engineer of a web-based in-house development company