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
+6.1k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+10.5k Golang : How to delete element(data) from map ?
+19.5k Golang : Set or Add HTTP Request Headers
+43k Golang : Convert []byte to image
+46.3k Golang : Marshal and unmarshal json.RawMessage struct example
+22k Golang : Convert seconds to minutes and remainder seconds
+9.1k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+14.8k Golang : Basic authentication with .htpasswd file
+6.5k Golang : How to validate ISBN?
+9.2k Golang : How to control fmt or log print format?
+23.8k Golang : Use regular expression to validate domain name
+7.2k Golang : Of hash table and hash map