Golang 四捨五入

Simple code

package main
 
import (
	"log"
)
 
func round(v float64, decimals int) float64 {
     var pow float64 = 1
     for i:=0; i<decimals; i++ {
         pow *= 10
     }
     return float64(int((v * pow) + 0.5)) / pow
}
 
func main() {
	log.Println(round(-123.555555, 3))
	log.Println(round(123.558, 2))
}

Output

2009/11/10 23:00:00 -123.555
2009/11/10 23:00:00 123.56