How to display 0 on the left side of the standard input value

environment

Mac Ruby 2.6.5

What you want to achieve

If the standard input number is less than the specified number of digits, 0 is added left-justified. Example) In the case of 8 digits 3→00000003 12→00000012 1234→00001234

Solution

Use the printf method. The format is specified by the first argument, the character string or numerical value given by the subsequent arguments is formatted according to the format, and the character string is generated and output. The basic printf method format is as follows.

printf ("% [flag] [width] [.precision] [indicator]", [string or number])

There are various flags, but if you use "0", the extra digits when specifying the width will be filled with "0". Width specifies the number of digits to be displayed. (Accuracy is not specified this time) There are various directives (instructing how to format given data), but when displaying an integer in decimal notation, use % d.

For example, in the above example:

num = gets.to_i
 printf ("% 08d", num) #Enter "0" for the flag, "8" for the width, and "% d" for the specifier

# When num = 3
=> "00000003"
# When num = 12
=> "00000012"

Recommended Posts

How to display 0 on the left side of the standard input value
How to display the result of form input
[Java] How to get the maximum value of HashMap
How to display the select field of time_select every 30 minutes
How to disable Set-Cookie from API on the front side
How to change the setting value of Springboot Hikari CP
[Rails] How to display the list of posts by category
How to check the database of apps deployed on Heroku
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to display products by category on the same list screen
[Rails] How to omit the display of the character string of the link_to method
How to increment the value of Map in one line in Java
Display text on top of the image
Samshin on the value of the hidden field
How to determine the number of parallels
How to change the timezone on Ubuntu
How to sort the List of SelectItem
How to pass the value to another screen
Pre-processing to display on the browser (compiler)
How to output the sum of any three numbers, excluding the same value
Recorded because I was addicted to the standard input of the Scanner class
How to switch the display of the header menu for each transition page
How to solve the local environment construction of Ruby on Rails (MAC)!
How to change the value of a variable at a breakpoint in intelliJ
Android development, how to check null in the value of JSON object
[Ruby On Rails] How to search the contents of params using include?
How to find the cause of the Ruby error
[Ruby on Rails] How to display error messages
Customize how to divide the contents of Recyclerview
Make a margin to the left of the TextField
[Java] How to get and output standard input
How to get today's day of the week
Output of how to use the slice method
[Ruby] Code to display the day of the week
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
[ruby] How to receive values from standard input?
[Ruby on Rails] How to make the link destination part of the specified id
How to display the amount of disk used by Docker container for each container
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
Pay attention to the boundary check of the input value when using the float type
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to share on the host side (windows) and guest side (CentOS 7) with VirtualBox
[Java] How to get the URL of the transition source
[Ubuntu 20.04] Display the day of the week on the date / clock
How to delete / update the list field of OneToMany
Speed comparison when the value side of Hash wants to retrieve all with array
How to set the display time to Japan time in Rails
[Ruby] Summary of how to get values from standard input [Paiza skill check measures]
The mark on the right side of the version-controlled Xcode file
[Rails, JS] How to implement asynchronous display of comments
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby] How to find the sum of each digit
[Ruby on Rails] How to change the column name
How to install the root certificate of Centos7 (Cybertrust)
I want to display the name of the poster of the comment
[Rails] How to change the column name of the table
[SwiftUI] How to specify the abbreviated position of Text
As of April 2018 How to get Java 8 on Mac
[ruby] How to assign a value to a hash by referring to the value and key of another hash