Home >Backend Development >Golang >How Do I Convert a float64 to an int in Go?
Converting float64 to int in Go using Type Casting
In Go, converting a float64 to an int can be achieved using type casting. Type casting, also known as type conversion, allows you to convert a value from one data type to another.
The syntax for type casting in Go is:
varName := type(expression)
Where varName is the name of the variable to store the converted value, type is the target data type, and expression is the expression or value to be converted.
In the case of converting a float64 to an int, the code would be:
var x float64 = 5.7 var y int = int(x) fmt.Println(y)
The above code declares a float64 variable x with an initial value of 5.7. It then declares an int variable y and converts the value of x to an int using type casting. Finally, the value of y is printed, which will output "5" since the fractional part of the float is discarded during the conversion.
The above is the detailed content of How Do I Convert a float64 to an int in Go?. For more information, please follow other related articles on the PHP Chinese website!