LoginSignup
13
9

More than 5 years have passed since last update.

Swiftでprintfのような感じで数字をフォーマットする

Posted at
print(1)
// 結果 -> 1

print(String(format: "%02d", 1))
// 結果 -> 01

例:

var year  = 2015
var month = 1
var day   = 31
var hour  = 1
var min   = 6
print("今の年月日日時は  \(String(format: "%04d", year))/\(String(format: "%02d", month))/\(String(format: "%02d", day))  \(String(format: "%02d", hour)):\(String(format: "%02d", min))")

// 結果 -> 今の年月日日時は  2015/01/31  01:06
13
9
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
13
9