Big prec lost at strconv.FormatFloat


(吕华廷) #1

As the posted code:

	fmt.Println(strconv.FormatFloat(220.355, 'g', 30, 64))
	fmt.Println(strconv.FormatFloat(0.001, 'g', 30, 64))

The output is:

220.354999999999989768184605055
0.00100000000000000002081668171172

Note that the origin number ‘220.355’,if i want to recover this number ,i have to do round with prec 13.
I have expected that the float64 have been implemented a prec at least 15!!!,so i always used round(15) to recover my real number util today. 15 is just meet my requirement,but 13…Ofcourse no!

So if i want to reach a meet-requirement precision. i have to use string or (bigint+exp)?


(Norbert Melzer) #2

You can round as much as you want, both numbers you are trying are not representable accurately, if they where you’d see only 0 after your last given digit.

If you need any kind of precision, simply do not use floats. Try some fixed point number type.