Home  >  Article  >  Backend Development  >  Why does go give me this result?

Why does go give me this result?

王林
王林forward
2024-02-10 12:48:091014browse

为什么 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")
}

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

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 5x102cdea26b4c3988e37d674b56660962a712eb235f530502820017cce1685312d59, 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)

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete