[LINUX] Ruby number output 0 padding

Display the calculation result in Ruby with 0 padding (3 → 003)

This time, as an example, consider a number that is double the input value with three digits padded with zeros. 1→002 2→004 6→012

1. First, normal display

1→2 2→4 6→12 First, enable

 puts "please put numbers"
number = gets.to_i
number2=number*2
print number2

↑ This completes.

2.0 Think about filling

sprintf("%03d",number)

Or

"%03d" % number

However, the number is displayed in 3 digits with 0 padding.

Program to run

 puts "please put numbers"
number = gets.to_i
number2=number*2
number3=sprintf("%03d", number2)
print number3

1→002 2→004 6→012

Was confirmed

References

http://osishow3.hateblo.jp/entry/2017/04/24/235500

https://www.setoya-blog.com/entry/2013/12/06/171143

Recommended Posts

Ruby number output 0 padding
numpy> Random number list output> zip () / print%