gpt4 book ai didi

go - big.Float GetString 没有四舍五入

转载 作者:IT王子 更新时间:2023-10-29 01:08:27 24 4
gpt4 key购买 nike

我想看结果:没有“3891113451447590400”的“3891113451447590234”

bigI,_ := big.NewInt(0).SetString("3891113451447590234", 10)
bigF := big.NewFloat(0).SetInt(bigI)
fmt.Println(bigF)
fmt.Println(bigF.String())
fmt.Println(bigF.SetMode(big.AwayFromZero).Text('f', 8))
fmt.Println(bigF.SetMode(big.AwayFromZero).Text('g', 20))

3.8911134514475904e+18
3.891113451e+18
3891113451447590400.00000000
3891113451447590400

最佳答案

big.NewFloat 函数将默认精度设置为 53。

NewFloat allocates and returns a new Float set to x, with precision 53 and rounding mode ToNearestEven. NewFloat panics with ErrNaN if x is a NaN.

如果你想设置精度更高的值,你可以直接设置精度,或者你可以使用 big.Float 的零值从精度 0 开始,这决定了当值时所需的精度是第一组。

f1, _, _ := new(big.Float).SetPrec(128).SetMode(big.ToNearestEven).Parse("3891113451447590234", 10)
// equivalent to
// big.ParseFloat("3891113451447590234", 10, 128, big.ToZero)
fmt.Println(f1)
// 3.891113451447590234e+18

i, _ = new(big.Int).SetString("3891113451447590234", 10)
f2 = new(big.Float).SetInt(i)
fmt.Println(f2)
// 3.891113451447590234e+18

关于go - big.Float GetString 没有四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51211983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com