简体   繁体   English

Float64类型在Golang中打印为int

[英]Float64 type printing as int in Golang

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. 我尝试简单地在Go中初始化一个float64并进行打印,然后尝试进行字符串转换并进行打印。 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 (eg num := 1.5 then fmt.Println(num) gives output 1 ). 我尝试了很多分数,包括那些不解析为重复小数的分数,以及简单地写出float和fmt.Println(num)例如num := 1.5然后fmt.Println(num)给出输出1 )。

package main

import (
    "fmt"
    "strconv"
)

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

Expected: 预期:

// Output:
1.66
1.66

Actual: 实际:

// Output:
1
1

The Go Programming Language Specification Go编程语言规范

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. 指数部分是e或E,后跟一个可选的带符号十进制指数。 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和y,整数商q = x / y且余数r = x%y满足以下关系:

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

with x / y truncated towards zero. x / y截断为零。


You wrote, using integer literals and arithmetic (x / y truncates towards zero): 您使用整数文字和算术(x / y截断为零)编写的:

package main

import (
    "fmt"
    "strconv"
)

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

Playground: https://play.golang.org/p/PBqSbpHvuSL 游乐场: 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\n", num)
    numString := strconv.FormatFloat(num, 'f', -1, 64)
    fmt.Println(numString)
}

Playground: https://play.golang.org/p/Hp1nac358HK 游乐场: https : //play.golang.org/p/Hp1nac358HK

Output: 输出:

1.6666666666666667
1.6666666666666667

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM