LoginSignup
7
1

More than 3 years have passed since last update.

Golangでのゼロ埋め

Posted at

Golangでのゼロ埋め

0~9の場合は数字の前に0を付け、
10以上の場合は0を付けない場合は以下の様に実現できる

s := 3
str := fmt.Sprintf("%02d", s)
fmt.Println(str)
// 03

s2 := 13
str2 := fmt.Sprintf("%02d", s2)
fmt.Println(str2)
// 13


知らずに以下の様な回りくどい方法で実現したので知ったときは衝撃だった...

if num < 10 {
​
    numStr = "0" + strconv.Itoa(num)
​
} else {
​
    numStr = strconv.Itoa(num)
​
}
7
1
1

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
7
1