Home  >  Article  >  Backend Development  >  How to convert integers to floating point numbers in Go language

How to convert integers to floating point numbers in Go language

王林
王林Original
2021-02-04 15:47:265008browse

Go language's method of converting integers into floating point numbers: [float32(int_expression)]. Type conversion is used to convert variables of one data type into variables of another type. The basic format is [type_name(expression)].

How to convert integers to floating point numbers in Go language

The operating environment of this article: windows10 system, Go 1.11.2, thinkpad t480 computer.

Type conversion is used to convert variables of one data type into variables of another type. The basic format of Go language type conversion is as follows:

type_name(expression)

type_name is the type and expression is the expression.

Example

In the following example, the integer type is converted into a floating point type, the result is calculated, and the result is assigned to a floating point variable:

package main

import "fmt"

func main() {
   var sum int = 17
   var count int = 5
   var mean float32
   
   mean = float32(sum)/float32(count)
   fmt.Printf("mean 的值为: %f\n",mean)
}

The above example executes the output result For:

mean 的值为: 3.400000

Related recommendations: golang tutorial

The above is the detailed content of How to convert integers to floating point numbers in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn