简体   繁体   English

big.Float GetString没有舍入

[英]big.Float GetString without rounding

I want to see the result: "3891113451447590234" without "3891113451447590400" 我想看到结果:“3891113451447590234”没有“3891113451447590400”

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

The big.NewFloat function sets the default precision to 53. big.NewFloat函数将默认精度设置为53。

NewFloat allocates and returns a new Float set to x, with precision 53 and rounding mode ToNearestEven. NewFloat分配并返回一个新的Float设置为x,精度为53,舍入模式为ToNearestEven。 NewFloat panics with ErrNaN if x is a NaN. 如果x是NaN,则NewFloat使用ErrNaN发生混乱。

If you want to set values with higher precision, you can set the precision directly, or you can start with precision 0 using a zero value of big.Float which determines the required precision when the value is first set. 如果要设置更高精度的值,可以直接设置精度,也可以使用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

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

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