Find the difference from a multiple of 10

What I've been working on right now It was to enter a positive integer and output True if it was less than or equal to a multiple of 10 and False otherwise.

Below is my answer.

def near_ten(num)
  basis = (num + 5) / 10 * 10
  difference = (num - basis).abs
  if difference <= 2
    puts "True"
  else
    puts "False"
  end
end

First, let basis be the multiple of 10 that is closest to num.

The second line calculates the numbers suitable for basis.

  basis = (num + 5) / 10 * 10

As an example, if it is an integer from 15 to 24, the nearest multiple of 10 is 20, so I want to use 20 as a reference. Add 5 to each of these integers to get 20-29. These integers have 2 in the tens place, so if you do / 10 * 10 respectively, 20 will come out, which is the number you want to use as a reference.

Other integers work just as well.

This completes the basis part.

Next, the difference between the original number and this standard is calculated.

num - basis

Since the difference may be negative as it is, we use a method that gives an absolute value.

(num - basis).abs

Then substitute this

difference = (num - basis).abs

If the difference difference obtained in this way is 2 or less, it is sufficient, so it is judged and the process ends.

  if difference <= 2
    puts "True"
  else
    puts "False"
  end

Recommended Posts

Find the difference from a multiple of 10
Find the average age from List <Person>.
Find the difference from a multiple of 10
Output the sum of each name and its contents from a multiple array
Extract a specific element from the list of objects
A record of studying the Spring Framework from scratch
Determine that the value is a multiple of 〇 in Ruby
Find the number of days in a month with Kotlin
Find the difference between List types
I made a tool to output the difference of CSV file
[Challenge CircleCI from 0] Learn the basics of CircleCI
Ruby from the perspective of other languages
Find the average age from List <Person>.
How to find out the Java version of a compiled class file
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Java] Generate a narrowed list from multiple lists using the Stream API
How to find the cause of the Ruby error
Rails: A brief summary of find, find_by, where
Has the content of useBodyEncodingForURI changed from Tomcat8?
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
Make a margin to the left of the TextField
True / false judgment based on the condition of each difference with multiple integers
Measure the size of a folder in Java
Set the time of LocalDateTime to a specific time
A fledgling engineer learned JUnit from the basics
Shows how many years and months the difference from a particular date is
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
Find the greatest common divisor and least common multiple of any number of integers in Ruby
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
ArrayList and the role of the interface seen from List
Count the number of occurrences of a string in Ruby
3. Create a database to access from the web module
[Java] I want to calculate the difference from the date
Volume 3 types of Docker Compose considered from the purpose
How to write Scala from the perspective of Java
About the usefulness of monads from an object-oriented perspective
[Ruby] How to find the sum of each digit
The story of migrating from Paperclip to Active Storage
The story of making a reverse proxy with ProxyServlet
Calculate the difference between numbers in a Ruby array
Find the remainder divided by 3 without using a number
[Java] When writing the source ... A memorandum of understanding ①
I wrote a sequence diagram of the j.u.c.Flow sample
Using the database (SQL Server 2014) from a Java program 2018/01/04
Display the average value of the evaluation as a star
A review note of the Spring Framework Resource interface
Find the approximate value of log (1 + x) in Swift
I learned about the existence of a gemspec file
A survey of the Kubernetes native Java framework Quarkus
The world of clara-rules (2)
A summary of what Java programmers find when reading Kotlin source for the first time
The story of migrating a stray batch without an owner from EC2 to a Docker environment
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
Registration of multiple WebMvcConfigurers
The idea of jQuery
The story of raising Spring Boot from 1.5 series to 2.1 series part2
A memorandum of the environment variable "JAVA_HOME" path setting procedure
Find the address class and address type from the IP address with Java