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
+12.4k Golang : Date and Time formatting
+9.1k Golang : Setting variable value with ldflags
+29.2k Golang : Remove characters from string example
+5.2k PHP : How to check if an array is empty ?
+10.2k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+11.7k Golang : Exit, terminating or aborting a program
+9.1k Golang : Detect number of faces or vehicles in a photo
+7.3k Golang : Get final or effective URL with Request.URL example
+8.8k Golang : Eroding and dilating image with OpenCV example
+17.7k Unmarshal/Load CSV record into struct in Go
+13.2k Golang : Chunk split or divide a string into smaller chunk example
+11k Golang : Get remaining text such as id or filename after last segment in URL path