How to round float to integer value in Go

1 Answer

0 votes
package main 
   
import ( 
    "fmt"
    "math"
) 
   
func main() { 
 	res_1 := math.Round(234.8745) 
    res_2 := math.Round(-12.99) 
    res_3 := math.Round(-0.6) 
    res_4 := math.Round(-0.4) 
    res_5 := math.Round(0.4) 
    res_6 := math.Round(0.6) 
    res_7 := math.Round(0.5) 
    res_8 := math.Round(1.5) 
   
    fmt.Printf("%.1f\n", res_1) 
    fmt.Printf("%.1f\n", res_2) 
    fmt.Printf("%.1f\n", res_3) 
    fmt.Printf("%.1f\n", res_4) 
    fmt.Printf("%.1f\n", res_5) 
    fmt.Printf("%.1f\n", res_6) 
    fmt.Printf("%.1f\n", res_7) 
    fmt.Printf("%.1f\n", res_8) 
} 
 
 
 
/*
run:
 
235.0
-13.0
-1.0
-0.0
0.0
1.0
1.0
2.0
 
*/

 



answered Aug 4, 2020 by avibootz

Related questions

1 answer 171 views
1 answer 164 views
1 answer 152 views
1 answer 104 views
1 answer 55 views
1 answer 66 views
1 answer 62 views
...