PHP rounding problem (5.2.3)?

前端 未结 3 1419
时光取名叫无心
时光取名叫无心 2021-01-12 11:10

I\'m wondering if I found an issue with the rounding in PHP, specifically 5.2.3 (I\'m not sure about other versions at the moment):

$t = 0;

$taxAmount = (5.         


        
相关标签:
3条回答
  • 2021-01-12 11:45

    You can use PHP_ROUND_HALF_UP, but there's a issue that number self transform to "99999..."

    0 讨论(0)
  • 2021-01-12 11:49

    Floats are evil.

    Quoting the PHP manual documentation on Floating Point numbers:

    So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

    If you want to know why and how floats work I recommend watching:
    Everything you didn't want to know about JavaScript numbers

    0 讨论(0)
  • 2021-01-12 12:03

    Python says:

    >>> repr(5./100*0.7)
    '0.034999999999999996'
    

    This is due to IEEE754 accuracy limitations. Use a fixed-point type if you need exact accuracy.

    0 讨论(0)
提交回复
热议问题