3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby数字出力 0埋め

Last updated at Posted at 2019-12-24

#Rubyでの計算結果を0埋めで表示(3→003)

今回、例として、入力された数値の倍の数を三桁の0埋めで考える。
1→002
2→004
6→012

##1.まず普通の表示
1→2
2→4
6→12
をまず、できるようにする

puts "数字を入れてください"
number = gets.to_i
number2=number*2
print number2

↑これで、完了。

##2.0埋めを考えていく

sprintf("%03d",number)

"%03d" % number

が、numberの0埋め3桁表示となる。

##実行するプログラム

puts "数字を入れてください"
number = gets.to_i
number2=number*2
number3=sprintf("%03d", number2)
print number3

1→002
2→004
6→012

が確認できた

##参考文献

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?