How to zero pad on the right side? This code zero pad to the left
package main
import "fmt"
func main() {
s := fmt.Sprintf("s", "45")
fmt.Println(s)
}
Output
000045
What I want
450000
CodePudding user response:
You can space pad on the right with fmt.Sprintf("%-6s", "45")
You must do your own padding to pad with 0
:
// Zero PAD Right
func zpadr(s string, n int) string {
n -= len(s)
if n > 0 {
s = strings.Repeat("0", n)
}
return s
}
Use like this: fmt.Sprintf("%s", zpadr("45", 6))