Why does go give me this result?

王林
Release: 2024-02-10 12:48:09
forward
1015 people have browsed it

为什么 go 给我这个结果?

php Xiaobian Xigua is here to answer a common question: "Why does go give me this result?" Go language is a programming language with the characteristics of efficiency and simplicity. It is widely used in network services, cloud computing and other fields. When developing in the Go language, you sometimes encounter some unexpected results. This may be caused by code logic problems, data processing errors, or some special circumstances. Understanding the characteristics and common problems of the Go language can help developers better solve and debug programs and improve development efficiency. In the following articles, we will answer some common questions and share some solutions and tips.

Question content

I am making a program to calculate the percentage of males and females in a class. But it gives me an incorrect result.

The code is:

package main
import {
    "fmt"
}

var total, mujeres, hombres float64

func main() {
    fmt.printf("número de mujeres:")
    fmt.scanln(&mujeres)

    fmt.printf("número de hombres:")
    fmt.scanln(&hombres)

    total = mujeres + hombres
    mujeres = (mujeres / total) * 100
    hombres = (hombres / total) * 100

    print("en al salón de clases hay ", mujeres, "% de mujeres y ", 
        hombres, "% de hombres")
}
Copy after login

The output obtained when inputting two quantities of 50 is:

En al salón de clases hay +5.000000+001% de mujeres y +5.000000+001% de hombres
Copy after login

I would like to know what causes this problem and how to solve it.

Workaround

Instead of giving the wrong result, it gives the correct result in the wrong format. Value 5.000000e 001 is 5x10<sup>1</sup>, which is equal to 50.

If you want them to be in a different format than the default, you need to specify, for example:

fmt.Printf("En al salón de clases hay %.1f%% du mujeres y %.1f%% du hombres\n",
    mujeres, hombres)
Copy after login

The above is the detailed content of Why does go give me this result?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!