Golang:如何在打印时在数字上加上零?


Answers:


176

fmt软件包可以为您做到这一点:

fmt.Printf("|%06d|%6d|\n", 12, 345)

注意%06d中的0,这将使其宽度为6并用零填充。第二个将填充空格。

您可以在这里查看其运行情况:http : //play.golang.org/p/cinDspMccp


2
如果我希望垫子在正确的位置怎么办?使用该标志-仅给出空格,我需要零。
majidarif

5
如果您希望以后再使用字符串,请使用Sprintf代替Printf
Oliverpool'Jan

42

使用Printf从功能FMT包装width6和填充字符0

import "fmt"
fmt.Printf("%06d", 12) // Prints to stdout '000012'

width通过将整数直接放在格式说明符(“动词”)之前来设置作品:

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'

Golang(和大多数其他语言)唯一支持的填充字符是空格和0

fmt.Printf("%6d", 12)   // Default padding is spaces, prints '    12'
fmt.Printf("%06d", 12)  // Change to 0 padding,       prints '000012'

可以通过在前面加上减号来使打印正确对齐-

fmt.Printf("%-6d", 12)   // Padding right-justified, prints '12    '

请注意,对于浮点数,宽度包括整个格式字符串:

fmt.Printf("%6.1f", 12.0) // Prints '0012.0' (width is 6, precision is 1 digit)

请注意,也可以通过编程方式设置宽度,方法是使用*而不是数字,并将宽度作为int参数传递:

myWidth := 6
fmt.Printf("%0*d", myWidth, 12) // Prints '000012' as before

例如,如果仅在运行时才知道要打印的最大值(maxVal在以下示例中调用),这可能会很有用:

myWidth := 1 + int(math.Log10(float64(maxVal)))
fmt.Printf("%*d", myWidth, nextVal)

最后,如果你不想打印到stdout,但返回一个字符串,使用Sprintf也从FMT包具有相同的参数:

s := fmt.Sprintf("%06d", 12) // returns '000012' as a String

29

有一种最简单的方法可以实现此目的。用

func padNumberWithZero(value uint32) string {
    return fmt.Sprintf("%02d", value)
}

fmt.Sprintf格式化并返回字符串,而不在任何地方打印。在这里%02d说,对于小于<2位数的值,在左边填充零。如果给定值有2个或更多数字,它将不填充。例如:

  • 如果输入为1,则输出为01。
  • 如果输入为12,则输出为12。
  • 如果输入为1992,则输出为1992。

您可以使用%03d或更多进行更多零填充。


10

问题“ 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)

play.golang.org

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

埃里克·帕尔默(Eric Palmer)用golang示例删除了他的仓库,因此链接不再起作用。
康斯坦丁·阿齐佐夫'17

1
@KonstantinAzizov好点。我已经恢复了该链接。有点。
VonC

3
func lpad(s string,pad string, plength int)string{
    for i:=len(s);i<plength;i++{
        s=pad+s
    }
    return s
}

lpad(“ 3”,“ 0”,2)结果:“ 03”

lpad(“ 12”,“ 0”,6)结果:“ 000012”

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.