Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong multiplication in PHP

I'm trying to do a simple operation of two numbers but the operation returns a wrong result.

The number are for example 46.29 and 10. The first one in the $a variable and the second one in the $b variable.

Process

echo $a * $b

returns 460

echo 10 * 46.29

print the right number (462.90)

$a * 10

and

46.29 * $b

result is always the same: 460!

echo $a

print 46,29

echo $b

print 10

echo floatval($a) * floatval($b)

print 460

echo intval($a)

print 46

I tried also to use bcmul, and in this case it will print 0.

Here you can find my code:

include 'simple_html_dom.php';

     $anno = $_POST['Anno'];
     $punti = $_POST['Punti'];
     $eta = $_POST['Eta'];
     $ggAss = $_POST['GgAss'];
     $ggParz1 = $_POST['GgParz1'];
     $ggParz2 = $_POST['GgParz2'];
     $ggParz3 = $_POST['GgParz3'];
     $pctDM = $_POST['PctDM'];
     $calcoloDM = $_POST['CalcoloDannoMorale'];
     $speseMediche = $_POST['SpeseMediche'];
     $spese = $_POST['Spese'];

    $html = file_get_html('..\tabella'.$anno.'.php');

    $rows = $html->find('tr');

    // This variable is use to make sure that the correct number will be display. (there is a kind of offset in the output table).
    $const = 2;


    $i = 0;
    $cond = false;
    foreach ($rows as $row) {
        $j = 0;
        foreach ($row->children() as $cell) {
            if($cond)
                break;
            //This condition is used to get punto base e indennità giornaliera
            if($i == 1) {
                $var1 = explode(" ", $cell->plaintext);
                $indennitaGG = $var1[10]; // here we can get indennità giornaliera
            }
            if($i == ($eta + $const) && $j == $punti) {
                $dannoBP = $cell->plaintext;
                $cond = true;
                break;
            }
            $j++;
        }
        $i++;
    }

    $calcIndGG = $indennitaGG * $ggAss;
    $newVar = $indennittaGG;
    echo 46.29 * 10;
    $calcIndParz1 = ($indennitaGG * 75 / 100) * $ggParz1;
    $calcIndParz2 = ($indennitaGG * 50 / 100) * $ggParz2;
    $calcIndParz3 = ($indennitaGG * 25 / 100) * $ggParz3;
    $dm = ($calcIndGG + $calcIndParz1 + $calcIndParz2 + $calcIndParz3) * $pctDM / 100;

    $totale = $dannoBP + $calcIndGG + $calcIndParz1 + $calcIndParz2 + $calcIndParz3 + $dm + $speseMediche + $spese;

What's the problem with this?

EDIT: PROBLEM SOLVED with this code:

//This change is used to transform the variable $indennitaGG in the right form. (with the . and not with the ,). Then we can make the cast to float.


    $temp = str_replace(",",".", $indennitaGG);
        $indennitaGG = (float)$temp;
like image 492
crasholino Avatar asked Mar 13 '23 15:03

crasholino


2 Answers

The , character is not valid for a floating point number. Try to replace it with str_replace(",",".",$a);. Even try to do it with both numbers if you wish.

The "official" decimal separator (for PHP) is a point (.), that's why it fails doing the multiplication. In your language it might be the comma, but PHP uses a dot.

This is what PHP would say (to floatval($a)):

Ok, I got two characters, which is a valid number (46), but after that there's a weird symbol, I'll just throw a warning, add a decimal point and a zero (cause I found no other decimal points), and end the conversion here.

If you use this code, it should work:

str_replace(",",".",$a);
str_replace(",",".",$b);
echo floatval($a) * floatval($b);

Explanation:

As I said, you're using a decimal comma, which is invalid. What str_replace(original, replacement, subject) does is that it finds all occurrences of original in a string (here: subject) and replaces it with the replacement value. This results in replacing that decimal comma with a decimal point.

Example: 40,3 becomes 40.3

This still results in a String data type, which is obviously not a number. To convert it, we use the method floatval(string), which results in a Float data type, which is short for Floating-point number and after that, we can perform arithmetical operations.

like image 168
ForceMagic Avatar answered Mar 16 '23 02:03

ForceMagic


Your locale is using a different sign for the decimal point. I suspect when you read the numbers from a file they are in the "wrong" format - e.q. 46,29 and not 46.29 as I can see from the echo.

Replace the , to . and then the issue is fixed.

like image 37
Peter VARGA Avatar answered Mar 16 '23 03:03

Peter VARGA