Sprintf

四舍六入:web

value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", 9.824), 64)
	fmt.Println(value) //9.82

	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", 9.826), 64)
	fmt.Println(value) //9.83

第三位为5且5以后有有效数字,知足五入:svg

value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", 9.8251), 64)
	fmt.Println(value) //9.83

	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", 9.8351), 64)
	fmt.Println(value) //9.84

第三位为5且5以后没有有效数字:
网上有人说,第二位为奇数则进位,第二位为偶数则舍去,例如:spa

value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", 9.825), 64)
	fmt.Println(value) //9.82

	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", 9.835), 64)
	fmt.Println(value) //9.84

可是:code

value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", 9.815), 64)
	fmt.Println(value) //9.81 竟然舍去了

	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", 9.845), 64)
	fmt.Println(value) //9.85 竟然进位了

因此,若是想知足正常的四舍五入逻辑,最好不要使用Sprintf处理。orm

math.Trunc

fmt.Println(math.Trunc(9.815*1e2+0.5)*1e-2) //9.82
	fmt.Println(math.Trunc(9.825*1e2+0.5)*1e-2) //9.83
	fmt.Println(math.Trunc(9.835*1e2+0.5)*1e-2) //9.84
	fmt.Println(math.Trunc(9.845*1e2+0.5)*1e-2) //9.85

以上结果显示符合四舍五入,可是偶尔会出现精度问题:xml

fmt.Println(math.Trunc(3.3*1e2+0.5)*1e-2) //3.3000000000000003
	fmt.Println(math.Trunc(3.3000000000000003*1e2+0.5) * 1e-2) //3.3000000000000003

一样使用Trunc,稍做调整:token

n10 := math.Pow10(2)
	fmt.Println(math.Trunc((9.815+0.5/n10)*n10) / n10) //9.82
	fmt.Println(math.Trunc((9.825+0.5/n10)*n10) / n10) //9.83
	fmt.Println(math.Trunc((9.835+0.5/n10)*n10) / n10) //9.84
	fmt.Println(math.Trunc((9.845+0.5/n10)*n10) / n10) //9.85
	fmt.Println(math.Trunc((3.3+0.5/n10)*n10) / n10) //3.3
	fmt.Println(math.Trunc((3.3000000000000003+0.5/n10)*n10) / n10) //3.3

符合四舍五入规则。string

若是要固定显示两位小数,需转换为string类型,前提是传入的数值,已经作过两位小数处理,不然依旧有进位问题:it

value := strconv.FormatFloat(3, 'f', 2, 64)
	fmt.Println(value) //3.00
	
	value = strconv.FormatFloat(3.3, 'f', 2, 64)
	fmt.Println(value) //3.30

	value = strconv.FormatFloat(9.815, 'f', 2, 64)
	fmt.Println(value) //9.81 被舍去

	value = strconv.FormatFloat(9.82, 'f', 2, 64)
	fmt.Println(value) //9.82

公众号:李田路口io