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

How to convert integers to floating point numbers in Go language

王林
Release: 2023-01-13 00:21:43
Original
5050 people have browsed it

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)
Copy after login

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)
}
Copy after login

The above example executes the output result For:

mean 的值为: 3.400000
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template