duanfu3390 2018-02-14 18:53
浏览 97
已采纳

将float转换为字符串而不更改float

I am converting a float to a string like this in Golang

a := strconv.FormatFloat(-3.6739403974420544e-15, 'f', -1, 64)

It's giving me this as output

-0.0000000000000036739403974420544

However, I would rather have this as output

   "-3.673940397442059e-15"

In the title to this question, I said "without altering" the float. By "altering" I am referring to the addition of the 0s in place of the e-15. is there a way to do it?

  • 写回答

2条回答 默认 最新

  • 普通网友 2018-02-14 18:58
    关注

    I think you are just using the wrong format specifier:

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        fmt.Println(strconv.FormatFloat(-3.6739403974420544e-15, 'e', -1, 64))
    }
    

    Output:

    -3.6739403974420544e-15
    

    The docs show the other formats as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义