'% 02d' What is the percentage of% 2?

'%02d' % 2

When you want to write the number 2 as '02' in ruby, you write a syntax like '% 02d'% 2. The method is to pad the argument with 0 and return it as a string, but what exactly is%, 02, or d here?

% What?

In ruby, the notation% has various meanings.

  1. "%" for remainder Example: 5% 3 => 2
  2. String class "%" operator Example:"% 02d "% 2 =>" 02 "
  3. Literal notation "%" operator Example:% w (dog cat monkey) => ["dog", "cat", "monkey"]
  4. Show input to the command line Example:% x [DATE] =>" Wednesday, June 24, 2020 09:31:57 JST \ n "

There are the above types. Reference: Meanings of symbols used in Ruby (excluding complex symbols in regular expressions)

The syntax '% 02d'% 2 shown above is equivalent to the 2. String class "%" operator in the above example!

What exactly is 02 or d?

d is one of the ** "indicators" ** that indicate the type of the argument. d represents a decimal number (Decimal number). Other directives include those that represent strings, those that represent floating point numbers, and those that represent exponential notation.

02 can be decomposed into 0 and 2, indicating ** "flag" ** and ** "width" **, respectively. There are several types of ** "flags" **, but 0 means to fill the margins with 0s. ** "Width" ** is left as it is, and the length at the time of output is specified.

To summarize these, the notation '% 02d'% 2 is

'% 0 (Please fill the margin with 0 ~) 2 (The length at the time of output is 2) d (Represent the argument as a decimal integer'% (% operator of String class Yo ~) 2 (The argument is 2!)

It means that it has the meaning.

in conclusion

Arguments can also be passed as an array, so ** When you say "I want to convert a decimal color code to a hexadecimal color code" ** You can write '% 02x% 02x% 02x'% [255, 255, 255] =>" ffffff "!

Recommended Posts

'% 02d' What is the percentage of% 2?
What is testing? ・ About the importance of testing
What is the data structure of ActionText?
What is JSP? ~ Let's know the basics of JSP !! ~
What is the pluck method?
What is the BufferedReader class?
What is the constructor for?
What is the initialize method?
What kind of StringUtil is good
What is the representation of domain knowledge in the [DDD] model?
What is it? ~ 3 types of "no" ~
What kind of method is define_method?
What is a Ruby 2D array?
Want to know what Ruby n is the power of 2? (Power judgment of 2)
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
What is the right migration data type? ??
What is the best file reading (Java)
What is the model test code testing
What is the main method in Java?
What are the updated features of java 13
What is the Facade pattern useful for?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Pullback doing in The Composable Architecture
ActiveSupport underscore is not the inverse of camelize
What is the Java Servlet / JSP MVC model?
The order of Java method modifiers is fixed
What is different from the PHP language. [Note]
What is the volatile modifier for Java variables?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What are the advantages of DI and Thymeleaf?
The official name of Spring MVC is Spring Web MVC
Java Welcome to the Swamp of 2D Arrays
Retrieve the first day of week in current locale (what day of the week is it today?)
What is Maven Assembly?
The essence of AspectJ's mood-why your `@Transactional` is ignored
Existence check of has_many and belongs_to-optional: What is true?