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
+4.3k Golang *File points to a file or directory ?
+28.8k Golang : Remove characters from string example
+5.1k Javascript : Get operating system and browser information
+33.8k Golang : Save image to PNG, JPEG or GIF format.
+7.7k Golang : Random integer with rand.Seed() within a given range
+8.2k Golang : Detect number of active displays and the display's resolution
+9.6k Golang : Get local time and equivalent time in different time zone
+4.9k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+12.9k Golang : syscall.Socket example
+9k CodeIgniter : Load different view for mobile devices
+4.6k Unix/Linux : How to find out the hard disk size?
+5.4k Golang : Break string into a slice of characters example