我有一个比较两个数字的简单 if 语句。由于编译错误,我无法使用big.Int与零进行比较,因此我尝试转换为 int64 和 float32。问题是在调用Int64or之后float32(diff.Int64()),diff被转换为一个正数,我怀疑这是整数溢出的结果。如果有人能告诉我什么是准备diff与零比较的变量的安全方法,我将不胜感激。
package main
import (
"fmt"
"math/big"
)
func main() {
var amount1 = &big.Int{}
var amount2 = &big.Int{}
amount1.SetString("465673065724131968098", 10)
amount2.SetString("500000000000000000000", 10)
diff := big.NewInt(0).Sub(amount1, amount2)
fmt.Println(diff)
// -34326934275868031902 << this is the correct number which should be compared to 0
fmt.Println(diff.Int64())
// 2566553871551071330 << this is what the if statement compares to 0
if diff.Int64() > 0 {
fmt.Println(float64(diff.Int64()), "is bigger than 0")
}
}
12345678_0001
相关分类