8
3

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.

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)
​
}
8
3
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
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?