Tra*_*der 150
fmt包可以为您执行此操作:
fmt.Printf("|%06d|%6d|\n", 12, 345)
Run Code Online (Sandbox Code Playgroud)
注意%06d中的0,这将使其宽度为6并用零填充它.第二个将用空格填充.
你可以在这里看到它:http://play.golang.org/p/cinDspMccp
Jai*_*rak 26
有一种最简单的方法可以实现这一目标.使用
func padNumberWithZero(value uint32) string {
return fmt.Sprintf("%02d", value)
}
Run Code Online (Sandbox Code Playgroud)
fmt.Sprintf
格式并返回一个字符串而不在任何地方打印它 这里%02d
说左边的垫零为具有<2位数的值.如果给定值有2位或更多位数,则不会填充.例如:
您可以使用%03d
更多或更多的零填充.
Chr*_*bek 23
使用fmt包中带有of 和padding字符的Printf
函数:width
6
0
import "fmt"
fmt.Printf("%06d", 12) // Prints to stdout '000012'
Run Code Online (Sandbox Code Playgroud)
设置宽度的工作方式如下:
fmt.Printf("%d", 12) // Uses default width, prints '12'
fmt.Printf("%6d", 12) // Uses a width of 6 and left pads with spaces, prints ' 12'
Run Code Online (Sandbox Code Playgroud)
width
在Golang中,大多数其他语言只支持空格和0
填充字符:
fmt.Printf("%6d", 12) // Default padding is spaces, prints ' 12'
fmt.Printf("%06d", 12) // Change to 0 padding, prints '000012'
Run Code Online (Sandbox Code Playgroud)
可以通过添加减号来右对齐打印-
:
fmt.Printf("%-6d", 12) // Padding right-justified, prints '12 '
Run Code Online (Sandbox Code Playgroud)
请注意,对于浮点数,宽度包括整个格式字符串:
fmt.Printf("%6.1f", 12.0) // Prints '0012.0' (width is 6, precision is 1 digit)
Run Code Online (Sandbox Code Playgroud)
值得注意的是,宽度也可以通过使用*
而不是数字并将宽度作为int
参数传递来以编程方式设置:
myWidth := 6
fmt.Printf("%0*d", myWidth, 12) // Prints '000012' as before
Run Code Online (Sandbox Code Playgroud)
作为可能有用的示例,请考虑您是否知道要打印的最大值maxVal
:
myWidth := 1 + int(math.Log10(float64(maxVal)))
fmt.Printf("%*d", myWidth, nextVal)
Run Code Online (Sandbox Code Playgroud)
最后,如果您不想打印stdout
但返回String,请使用Sprintf
:
s := fmt.Sprintf("%06d", 12) // returns '000012' as a String
Run Code Online (Sandbox Code Playgroud)
ULL*_*S K 13
以防万一,如果您想通过连接添加前缀或后缀来形成另一个单词,可以使用下面的代码。
package main
import "fmt"
func main() {
concatenatedWord:= "COUNTER_"+fmt.Sprintf("%02d", 1)
// use concatenatedWord
fmt.Println("ConcatenatedWordword is", concatenatedWord)
}
Run Code Online (Sandbox Code Playgroud)
输出:ConcatenatedWordword 是 COUNTER_01
链接: https: //play.golang.org/p/25g3L8TXiPP
" Go lang的印刷格式列表 "这个问题提醒我们,还有一面旗帜:
-
垫右边有空格而不是左边(左边对齐场)
DaddyOh/golang-samples/pad.go
如果你想填充其他字符串序列(比' 0
'或' '):
leftPad(s string, padStr string, pLen int)
rightPad(s string, padStr string, pLen int)
leftPad2Len(s string, padStr string, overallLen int)
rightPad2Len(s string, padStr string, overallLen int)
1234567890
leftPad(str, "*", 3) ***1234567890
leftPad2Len(str, "*-", 13) -*-1234567890
leftPad2Len(str, "*-", 14) *-*-1234567890
leftPad2Len(str, "*", 14) ****1234567890
leftPad2Len(str, "*-x", 14) x*-x1234567890
leftPad2Len(str, "ABCDE", 14) BCDE1234567890
leftPad2Len(str, "ABCDE", 4) 7890
rightPad(str, "*", 3) 1234567890***
rightPad(str, "*!", 3) 1234567890*!*!*!
rightPad2Len(str, "*-", 13) 1234567890*-*
rightPad2Len(str, "*-", 14) 1234567890*-*-
rightPad2Len(str, "*", 14) 1234567890****
rightPad2Len(str, "*-x", 14) 1234567890*-x*
rightPad2Len(str, "ABCDE", 14) 1234567890ABCD
rightPad2Len(str, "ABCDE", 4) 1234
Run Code Online (Sandbox Code Playgroud)
小智 7
func lpad(s string,pad string, plength int)string{
for i:=len(s);i<plength;i++{
s=pad+s
}
return s
}
Run Code Online (Sandbox Code Playgroud)
lpad("3","0",2) 结果:"03"
lpad("12","0",6) 结果:"000012"
归档时间: |
|
查看次数: |
58937 次 |
最近记录: |