PHP floating point precision issue

藏色散人
Release: 2023-04-07 20:04:02
forward
2838 people have browsed it

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
?>
Copy after login

Solution

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

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template