dougua3706 2018-10-01 06:41
浏览 1211
已采纳

Golang中的以大整数开头的零填充格式方法的用法示例

I would like to format a big int as a string with leading zeros. I'm looking for an example similar to this, but with Big:

I'm looking at source here.

But when I call:

m := big.NewInt(99999999999999)
fmt.Println(m.Format("%010000000000000000000","d"))

I see:

prog.go:10:22: m.Format("%010000000000000000000", "d") used as value
prog.go:10:23: cannot use "%010000000000000000000" (type string) as type fmt.State in argument to m.Format:
    string does not implement fmt.State (missing Flag method)
prog.go:10:48: cannot use "d" (type string) as type rune in argument to m.Format

(I understand normally I can use m.String(), but zero padding seems to complicate this, so I'm looking specifically for some help on the Format method.)

Here's my playground link.

  • 写回答

2条回答 默认 最新

  • dsm42026 2018-10-01 17:29
    关注

    You can simply use fmt.Sprintf(...) with the "%020s" directive (where 20 is whatever total length you want). The s verb will use the natural string format of the big int and the 020 modifier will create a string with total length of (at least) 20 with zero padding (instead of whitespace).

    For example (Go Playground):

    m := big.NewInt(99999999999999)
    s := fmt.Sprintf("%020s", m)
    fmt.Println(s)
    // 00000099999999999999
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信