Golang : Convert(cast) float to int
One of trait of a good programmer is laziness. There are times that I wish to have a complete source code for the project that I'm working on just by Googling for it. One of this is just simply ..... how to convert(cast) a float or float64 value to int type in Golang. :P
Without much further ado :
package main
import (
"fmt"
)
func main() {
var floatvalue float64 = 10.88
var intvalue int = int(floatvalue)
fmt.Println(intvalue)
// or
fmt.Println(int(floatvalue))
}
Output will be :
10
10
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.5k Android Studio : Hello World example
+11.7k Python : Convert IPv6 address to decimal and back to IPv6
+9.5k Golang : Create matrix with Gonum Matrix package example
+28.7k Get client IP Address in Go
+8.2k Golang : Detect number of active displays and the display's resolution
+19.3k Golang : Convert date string to variants of time.Time type examples
+18.3k Golang : Count JSON objects and convert to slice/array
+18.2k Golang : Measure http.Get() execution time
+8.8k Golang : Check if user agent is a robot or crawler example
+17.3k Golang : Get path name to current directory or folder
+18.1k Golang : How to run your code only once with sync.Once object
+6.2k Golang : Example of custom handler for Gorilla's Path usage.