Go:将float64转换为带有乘数的int [英] Go: Converting float64 to int with multiplier
问题描述
我想转换一个 float64
数字,假设它是 1.003
到 1003
(整型)。我的实现只是将 float64
与 1000
相乘,并将其转换为 int $ c
$ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
func main(){
var f float64 = 1.003
fmt.Println(int(f * 1000))
}
但是当我运行这个代码时,得到的是 1002
不是 1003
。由于Go自动将 1.003
存储为变量中的 1.002999 ...
。在Golang上做这种操作的正确方法是什么?
数字类型
将浮点数转换为整数时,分数将被丢弃(截断为零)。
因此,基本上当你将一个浮点数转换为一个整数时,只有整数部分被保留。
如果您只是想避免由有限位表示产生的错误,只需在将
0.5
转换为int
。
由于
float - > int
转换不是舍入,而是保留整数部分,这会给你想要的结果。考虑到可能的更小和更大的代表性:
1002.9999 + 0.5 = 1003.4999;整数部分:1003
1003.0001 + 0.5 = 1003.5001;整数部分:1003
所以简单写一下:
var f float64 = 1.003
fmt.Println(int(f * 1000 + 0.5))
把这个包装到一个函数中:
$ b $ pre $ func toint(f float64) int {
return int(f + 0.5)
}
//使用它:
fmt.Println(toint(f * 1000))
在 Go Playground 。
注意: 如果使用负数,请小心!例如,如果您的值
-1.003
,那么您可能希望结果为-1003
。但如果你添加0.5
:
$ p $-1002.9999 + 0.5 = -1002.4999;整数部分:-1002
-1003.0001 + 0.5 = -1002.5001;整数部分:-1002
所以如果你有负数,你必须:
- 减去
0.5
而不是添加它
- 或添加
0.5
但是从结果中减去1
$
$ bfunc toint(f float64)int {
if f< 0 {
return int(f - 0.5)
}
return int(f + 0.5)
$ / code $
I want to convert a
float64
number, let's say it1.003
to1003
(integer type). My implementation is simply multiply thefloat64
with1000
and cast it toint
.package main import "fmt" func main() { var f float64 = 1.003 fmt.Println(int(f * 1000)) }
But when I run that code, what I got is
1002
not1003
. Because Go automatically stores1.003
as1.002999...
in the variable. What is the correct approach to do this kind of operation on Golang?解决方案Conversions between numeric types
When converting a floating-point number to an integer, the fraction is discarded (truncation towards zero).
So basically when you convert a floating-point number to an integer, only the integer part is kept.
If you just want to avoid errors arising from representing with finite bits, just add
0.5
to the number before converting it toint
. No external libraries or function calls (from standard library) required.Since
float -> int
conversion is not rounding but keeping the integer part, this will give you the desired result. Taking into consideration both the possible smaller and greater representation:1002.9999 + 0.5 = 1003.4999; integer part: 1003 1003.0001 + 0.5 = 1003.5001; integer part: 1003
So simply just write:
var f float64 = 1.003 fmt.Println(int(f * 1000 + 0.5))
To wrap this into a function:
func toint(f float64) int { return int(f + 0.5) } // Using it: fmt.Println(toint(f * 1000))
Try them on the Go Playground.
Note:
Be careful when you apply this in case of negative numbers! For example if you have a value of
-1.003
, then you probably want the result to be-1003
. But if you add0.5
to it:-1002.9999 + 0.5 = -1002.4999; integer part: -1002 -1003.0001 + 0.5 = -1002.5001; integer part: -1002
So if you have negative numbers, you have to either:
- subtract
0.5
instead of adding it- or add
0.5
but subtract1
from the resultIncorporating this into our helper function:
func toint(f float64) int { if f < 0 { return int(f - 0.5) } return int(f + 0.5) }
这篇关于Go:将float64转换为带有乘数的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!