10 examples of 'golang round float' in Go

Every line of 'golang round float' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
110func roundF64(val float64, places int) float64 {
111 v, _ := strconv.ParseFloat(fmt.Sprintf("%0.3f", val), 64)
112 return v
113}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

107func RoundFloat(x float64, prec int) float64 {
108 var rounder float64
109 pow := math.Pow(10, float64(prec))
110 intermed := x * pow
111 _, frac := math.Modf(intermed)
112 x = .5
113 if frac < 0.0 {
114 x = -.5
115 }
116 if frac >= x {
117 rounder = math.Ceil(intermed)
118 } else {
119 rounder = math.Floor(intermed)
120 }
121
122 return rounder / pow
123}
67func Round(x complex128, prec int) complex128 {
68 if x == 0 {
69 // Make sure zero is returned
70 // without the negative bit set.
71 return 0
72 }
73 return complex(scalar.Round(real(x), prec), scalar.Round(imag(x), prec))
74}
150func RoundºFloatInt(val float64, digits int64) float64 {
151 dec := float64(1)
152 for ; digits > 0; digits-- {
153 dec *= 10
154 }
155 return math.Round(val*dec) / dec
156}
29func RoundFloat(f float64) float64 {
30 return math.RoundToEven(f)
31}
166func FloatRound(num float64, precision int) float64 {
167 for i := 0; i < precision; i++ {
168 num *= 10
169 }
170 temp := float64(int64(num))
171 for i := 0; i < precision; i++ {
172 temp /= 10.0
173 }
174 num = temp
175 return num
176}
123func RoundFloat(num float64, base string, precision int) float64 {
124 x := num
125 div := math.Pow10(precision)
126 switch base {
127 case "K":
128 x /= 1024
129 case "M":
130 x /= (1024 * 1024)
131 case "G":
132 x /= (1024 * 1024 * 1024)
133 }
134 return math.Round(x*div) / div
135}
171func roundf(x float64) float64 {
172 return math.Floor(0.5 + x)
173}
36func round(x float64, precision int) float64 {
37 var rounder float64
38 pow := math.Pow(10, float64(precision))
39 intermediate := x * pow
40
41 if intermediate < 0.0 {
42 intermediate -= 0.5
43 } else {
44 intermediate += 0.5
45 }
46 rounder = float64(int64(intermediate))
47
48 return rounder / float64(pow)
49}
748func roundSucc(v float64) float64 {
749 return math.Round(v*1000.0) / 1000.0
750}

Related snippets