问题描述

0.54999999999999720.55
0.54999999999999720.55
package main

import "fmt"

func main() {
    x := 100.55
    fmt.Println(x - float64(int(x)))
}

Output:
0.5499999999999972

推荐答案

100.55100.55100.55
100.55100.55100.55
100.55100.551/30.333333333....
100.55100.551/30.333333333....
float64float64
float64float64

现在当你说"这个:

x := 100.55
xxfloat64IEEE-754100.55100.55
xxfloat64IEEE-754100.55100.55
x := 100.55
fmt.Printf("%.50f
", x)

100.54999999999999715782905695959925651550292968750000
100.55
100.55
100float64(int(x))100.0
100float64(int(x))100.0
x = x - float64(int(x))
fmt.Printf("%.50f
", x)

0.54999999999999715782905695959925651550292968750000
0.55float64 类型的变量中获得精确的 0.55 数0.55
0.550.55float64
fmt.Printf()%f
fmt.Printf()%f
fmt.Printf("%.2f
", x)

结果:

0.55
100
100
cents := 10055
fmt.Printf("%d.%d $", cents/100, cents%100)

输出:

100.55 $

这篇关于Golang 将 float64 转换为 int 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!