dongye9071 2019-07-14 22:37 采纳率: 0%
浏览 430
已采纳

Float64类型在Golang中打印为int

Surprisingly I couldn't find anyone else having this same issue; I tried simply initializing a float64 in Go and printing it, then attempting a string conversion and printing that. Neither output was accurate.

I've attempted this with many fractions, including those which don't resolve to repeating decimals, as well as simply writing out the float and printing (e.g. num := 1.5 then fmt.Println(num) gives output 1).

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var num float64
    num = 5/3
    fmt.Printf("%v
", num)
    numString := strconv.FormatFloat(num, 'f', -1, 64)
    fmt.Println(numString)
}

Expected:

// Output:
1.66
1.66

Actual:

// Output:
1
1
  • 写回答

1条回答 默认 最新

  • duancongjue9202 2019-07-14 22:43
    关注

    The Go Programming Language Specification

    Integer literals

    An integer literal is a sequence of digits representing an integer constant.

    Floating-point literals

    A floating-point literal is a decimal representation of a floating-point constant. It has an integer part, a decimal point, a fractional part, and an exponent part. The integer and fractional part comprise decimal digits; the exponent part is an e or E followed by an optionally signed decimal exponent. One of the integer part or the fractional part may be elided; one of the decimal point or the exponent may be elided.

    Arithmetic operators

    For two integer values x and y, the integer quotient q = x / y and remainder r = x % y satisfy the following relationships:

    x = q*y + r  and  |r| < |y|
    

    with x / y truncated towards zero.


    You wrote, using integer literals and arithmetic (x / y truncates towards zero):

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        var num float64
        num = 5 / 3 // float64(int(5)/int(3))
        fmt.Printf("%v
    ", num)
        numString := strconv.FormatFloat(num, 'f', -1, 64)
        fmt.Println(numString)
    }
    

    Playground: https://play.golang.org/p/PBqSbpHvuSL

    Output:

    1
    1
    

    You should write, using floating-point literals and arithmetic:

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        var num float64
        num = 5.0 / 3.0 // float64(float64(5.0) / float64 (3.0))
        fmt.Printf("%v
    ", num)
        numString := strconv.FormatFloat(num, 'f', -1, 64)
        fmt.Println(numString)
    }
    

    Playground: https://play.golang.org/p/Hp1nac358HK

    Output:

    1.6666666666666667
    1.6666666666666667
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势