How to round a number to nearest int when casting float to int in Go

1 Answer

0 votes
package main 
   
import ( 
    "fmt"
) 
   
func main() { 
	f1 := 2.2
 	res_1 := int(f1 + 0.5) 
	
	f2 := 2.5
    res_2 := int(f2 + 0.5) 
	
	f3 := 2.8
    res_3 := int(f3 + 0.5) 
   
    fmt.Printf("%d\n", res_1) 
    fmt.Printf("%d\n", res_2) 
    fmt.Printf("%d\n", res_3) 
} 
 
 
 
/*
run:
 
2
3
3
 
*/

 



answered Aug 4, 2020 by avibootz

Related questions

1 answer 55 views
1 answer 174 views
174 views asked Aug 4, 2020 by avibootz
1 answer 226 views
226 views asked Oct 17, 2020 by avibootz
1 answer 145 views
145 views asked May 26, 2020 by avibootz
1 answer 66 views
1 answer 62 views
...