V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
rimutuyuan
V2EX  ›  问与答

go 语言 有很多位小数的 big.Float 转换成 string 类型如何不使用科学计数法。

  •  
  •   rimutuyuan · 2020-09-22 18:09:05 +08:00 · 1905 次点击
    这是一个创建于 1309 天前的主题,其中的信息可能已经有所发展或是发生改变。
    res, _ := new(big.Float).SetString("100000000000")
    res = res.Quo(res, big.NewFloat(1e18))
    fmt.Println(res.String())

    输出 1e-07

    如何输出 0.000001 呢
    2 条回复    2020-09-22 18:54:32 +08:00
    luwill
        1
    luwill  
       2020-09-22 18:39:26 +08:00   ❤️ 1
    https://golang.org/src/math/big/ftoa.go

    读一下 math/big 的源代码,你会发现 String 调用的 Text 方法。
    ```
    // String formats x like x.Text('g', 10).
    // (String must be called explicitly, Float.Format does not support %s verb.)
    func (x *Float) String() string {
    return x.Text('g', 10)
    }
    ```
    Text 支持 formt 参数,你要的`res.Text('f', 10)`

    // 'f' -ddddd.dddd, no exponent
    lysS
        2
    lysS  
       2020-09-22 18:54:32 +08:00
    你可以用这个
    a := 0.0000001
    fmt.Println(strconv.FormatFloat(float64(a), 'f', 9, 64))
    //
    0.000000100
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4019 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:16 · PVG 13:16 · LAX 22:16 · JFK 01:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.