Home  >  Article  >  Backend Development  >  PHP floating point precision issue

PHP floating point precision issue

藏色散人
藏色散人forward
2019-11-18 14:31:232786browse

When using php - */ to calculate floating point numbers, you may encounter some problems with incorrect calculation results, as follows:

<?php
    echo intval(0.58 * 100); //输出57
?>

Solution

<?php
    echo bcmul(0.58, 100); //输出58
?>

Recommended : "PHP Tutorial"

● PHP provides BC high-precision function library to handle this problem

● bcadd — Add two high-precision numbers

● bcsub — Subtract two high-precision numbers

● bcmul — Multiply two high-precision numbers

● bcdiv — Divide two high-precision numbers

● bccomp — Compares two high-precision numbers, returns -1, 0, 1

● bcmod — Finds the remainder of a high-precision number

● bcpow — Finds the power of a high-precision number

● bcpowmod - Find the power of high-precision numbers and find the modulus, very commonly used in number theory

● bcscale - Configure the default number of decimal points, equivalent to "scale=" in Linux bc

● bcsqrt — Find the square root of a high-precision number

The above is the detailed content of PHP floating point precision issue. For more information, please follow other related articles on the PHP Chinese website!

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