dongzhang6677 2018-10-09 03:31
浏览 522
已采纳

在Go中将值四舍五入到小数点后2位

Rounding positive value (example here: 1.015) half-up to 2 decimal places using math.Round() in Go:

fmt.Println(math.Round(1.015*100) / 100)

Go Playground

I got: 1.02. That's correct.

But when I employed a function to do the same job:

func RoundHalfUp(x float64) float64 {
    return math.Round(x*100) / 100
}

Go Playground

I got 1.01.

What's wrong with the RoundHalfUp function?

  • 写回答

1条回答 默认 最新

  • dongzhihong3940 2018-10-09 06:40
    关注

    The Go Programming Language Specification

    Constants

    Numeric constants represent exact values of arbitrary precision and do not overflow.

    Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:

    • Represent floating-point constants, including the parts of a complex constant, with a mantissa of at least 256 bits and a signed
      binary exponent of at least 16 bits.
    • Round to the nearest representable constant if unable to represent a floating-point or complex constant due to limits on precision.

    These requirements apply both to literal constants and to the result of evaluating constant expressions.

    Constant expressions

    Constant expressions may contain only constant operands and are evaluated at compile time.

    Constant expressions are always evaluated exactly; intermediate values and the constants themselves may require precision significantly larger than supported by any predeclared type in the language.

    Implementation restriction: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This rounding may cause a floating-point constant expression to be invalid in an integer context, even if it would be integral when calculated using infinite precision, and vice versa.


    Implement the RoundHalfUp function like the Go compiler does for math.Round(1.015*100) / 100. 1.015*100 is a untyped floating-point constant expression. Use the math/big package with at least 256 bits of precision. Go float64 (IEEE-754 64-bit floating-point) has 53 bits of precision.

    For example, with 256 bits of precision (constant expression),

    package main
    
    import (
        "fmt"
        "math"
        "math/big"
    )
    
    func RoundHalfUp(x string) float64 {
        // math.Round(x*100) / 100
        xf, _, err := big.ParseFloat(x, 10, 256, big.ToNearestEven)
        if err != nil {
            panic(err)
        }
        xf100, _ := new(big.Float).Mul(xf, big.NewFloat(100)).Float64()
        return math.Round(xf100) / float64(100)
    }
    
    func main() {
        fmt.Println(RoundHalfUp("1.015"))
    }
    

    Playground: https://play.golang.org/p/uqtYwP4o22B

    Output:

    1.02
    

    If we only use 53 bits of precision (float64):

    xf, _, err := big.ParseFloat(x, 10, 53, big.ToNearestEven)
    

    Playground: https://play.golang.org/p/ejz-wkuycaU

    Output:

    1.01
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类