简体   繁体   English

带两位小数的 Float64

[英]Float64 with two decimals

I am new at this.我对此很陌生。 I am trying to make a calculator that receives two numbers of type float64 but then I want to put the result of two decimals.我正在尝试制作一个接收两个float64类型数字的计算器,但我想输入两位小数的结果。 I have been reading that it is with "%.2f" but it does not take well the result in fmt.Scanf我一直在阅读它与"%.2f"一起使用,但在fmt.Scanf中的结果并不好

package main

import "fmt"

const menu string = "Calculator v1.0:\n\t1.a+b\n\t2.a-b\n\t3.a*b\n\t4.a/b\nSelect an option:"

func main() {
    var option int
    var firstNumber float64
    var secondNumber float64

    fmt.Println(menu)
    fmt.Scanf("%d", &option)

    fmt.Println("First number:")
    fmt.Scanf("%.2f", &firstNumber)
    fmt.Println("Second number:")
    fmt.Scanf("%.2f", &secondNumber)

    if option == 1 {
        println(sum(firstNumber, secondNumber))
    } else if option == 2 {
        println(subtract(firstNumber, secondNumber))
    } else if option == 3 {
        println(multiply(firstNumber, secondNumber))
    } else if option == 4 {
        println(divide(firstNumber, secondNumber))
    }

}

func sum(a float64, b float64) float64 {
    return a + b
}
func subtract(a float64, b float64) float64 {
    return a - b
}
func multiply(a float64, b float64) float64 {
    return a * b
}
func divide(a float64, b float64) float64 {
    return a / b
}

If you just need to accept a float 64 value for your inputs, you can simply use %f , or %g to set your values.如果您只需要接受一个浮点 64 值作为输入,您可以简单地使用%f%g来设置您的值。 The default precision is 6.默认精度为 6。

If you need greater precision and error handling, it may be more robust to bring your inputs in as strings, then parse and process them as you require.如果您需要更高的精度和错误处理,将输入作为字符串引入,然后根据需要解析和处理它们可能会更稳健。

Run the code below to see the inputs are both float64.运行下面的代码以查看输入都是 float64。 The first will be output with a precision of 2, the second will be output with the default precision.第一个是 output,精度为 2,第二个是 output,默认精度。

package main

import "fmt"

const menu string = "Calculator v1.0:\n\t1.a+b\n\t2.a-b\n\t3.a*b\n\t4.a/b\nSelect an option:"

func main() {
    var option int
    var firstNumber float64
    var secondNumber float64

    fmt.Println(menu)
    fmt.Scanf("%d", &option)

    fmt.Println("First number:")
    fmt.Scanf("%f", &firstNumber)
    fmt.Println("Second number:")
    fmt.Scanf("%g", &secondNumber)

    fmt.Printf("Input Type: %T, FirstNumber: %.2f\n", firstNumber, firstNumber)
    fmt.Printf("Input Type 2: %T, SecondNumber: %f\n", secondNumber, secondNumber)

    if option == 1 {
        println(sum(firstNumber, secondNumber))
    } else if option == 2 {
        println(subtract(firstNumber, secondNumber))
    } else if option == 3 {
        println(multiply(firstNumber, secondNumber))
    } else if option == 4 {
        println(divide(firstNumber, secondNumber))
    }

}

func sum(a float64, b float64) float64 {
    return a + b
}
func subtract(a float64, b float64) float64 {
    return a - b
}
func multiply(a float64, b float64) float64 {
    return a * b
}
func divide(a float64, b float64) float64 {
    return a / b
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM