Home > Backend Development > PHP Tutorial > PHP floating point comparison method, php floating point size_PHP tutorial

PHP floating point comparison method, php floating point size_PHP tutorial

WBOY
Release: 2016-07-12 08:59:24
Original
1135 people have browsed it

PHP floating point comparison method, php floating point size

The example in this article describes the PHP floating point comparison method. Share it with everyone for your reference, the details are as follows:

<&#63;php
/**
 * 浮点数一般是不能用来比较大小的,但是我们可以用一种变通的的方式
 * 用var_dump输出浮点是看不出效果的,可以用serialize查看
 * 1.round 2.浮点转换成字符串
 *
 * 转换成字符串方法:
 * 通过在其前面加上(string)或用strval()函数来转变成 字符串
 * 在一个需要字符串的表达式中,字符串会自动转变,比如在使用函数 echo() 或 print() 时, 或在一个变量和一个 字符串 进行比较时,就会发生这种转变
 * true会转为1 , 而false则会转为空字符串
 *
 */
$a = 13.2;
$b = 24;
$c = $a/$b;
//实际值是这个d:0.54999999999999993338661852249060757458209991455078125;
echo serialize($c).'<br/>';//
echo $c.'<br/>';//输出时会显示成0.55 实际的值是比他小的
//所以直接和0.55比较大小是不成立的
if($c == 0.55){
  echo 'nothing';
}
$c = round($c,2);
//用round处理
if($c == 0.55){
  echo 'ok';
}
echo "<br/>";
//强制转为字符串
// $c = (string)$c;
// $c = strval($c);
if("$c" == 0.55){
  echo 'ok';
}
&#63;>

Copy after login

The running results are as follows:

d:0.54999999999999993338661852249060757458209991455078125;
0.55
ok
ok

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operations and operator usage", "Introduction to PHP basic syntax", "Summary of PHP error and exception handling methods" and "PHP commonly used Summary of functions and techniques》

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • How to compare the date sizes of two strings in php
  • Examples of comparing time sizes in PHP
  • php comparison The size of two absolute times
  • PHP compares the size sorting implementation code of values ​​​​in multi-dimensional arrays
  • Detailed introduction of PHP comparison operators
  • Two float (floating point numbers) in PHP )Comparison example analysis
  • How to compare the length of two strings in php
  • PHP’s floating point calculation and comparison and the solution to inaccurate integer taking
  • php compare similar strings Method
  • Solution to inaccurate floating point calculation comparison and rounding in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099056.htmlTechArticlePHP floating point comparison method, php floating point size This article describes the PHP floating point comparison method in an example. Share it with everyone for your reference, the details are as follows: php/** * Floating point numbers are generally not...
Related labels:
source:php.cn
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