简体   繁体   中英

Why am I getting INF when dividing small numbers in PHP

I have two tables in a database. Each have a column (varchar255) with a small number (0-30). I'm only trying to divide those two and this is the result:
If one column has the number 6,575 and the other 1,291 the equation should be 5,09. It outputs 6 . Other/most results outputs INF

具有更多方程的图像

The numbers come from a foreach loop from the database and this is the code from the picture:

echo $row["ton"]." - ".$row_w["weight"]." - ".$row["ton"] / $row_w["weight"]."<br>";

I have tried bcdiv and that outputs nothing and is_infinite = 1. What am I missing?

You need to convert the values (char strings) to float using floatval before doing the division.

The inf means that the result is infinite because you are dividing by 0 or a very small number as a result of an unexpected value coming from dealing with chars as floats, as the program is not able to understand the decimals in a proper way.

Example:

$var = '578.23';

$float_value_of_var = floatval($var);

and your code could be something like this (only as an indication):

echo $row["ton"]." - ".$row_w["weight"]." - ".floatval($row["ton"]) / floatval($row_w["weight"])."<br>";

Thanks @aynber/@Ash-b for seeing my badly stored values.

$a = str_replace(",", ".", $row["ton"]);
$b = str_replace("," ,".", $row_w["weight"]);

echo $row["ton"]." - ".$row_w["weight"]." - ".$a / $b."<br>";

. instead of ,

Can't set a comment to answer, but case solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM