Invalid Multiplication in PHP

I am trying to perform a simple two-number operation, but the operation returns an incorrect result.

This number, for example, 46.29and 10. The first is in the variable $a, and the second is in the variable $b.

Process

echo $a * $b

returns 460

echo 10 * 46.29

print the right number (462.90)

$a * 10

and

46.29 * $b
Result

always the same: 460!

echo $a

print 46.29

echo $b

type 10

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

seal 460

echo intval($a)

print 46

I also tried using bcmul, in which 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 is 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;
+4
source share
4 answers

, . str_replace(",",".",$a);. , .

"" ( PHP) - (.), . , PHP .

, PHP ( floatval($a)):

, , (46), , , ( ) .

, :

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

:

, , . str_replace(original, replacement, subject), , (: subject) . .

: 40,3 40.3

- String, , , . , floatval(string), Float, , .

+4

locale . , , "" - e.q. 46,29, 46.29, echo.

, ., .

+3

, .

$a = 10;
$b = 46.29;
echo $a * $b;

echo $a $b, . , .

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

, , . post include. , .

0

float . , , . , , var_dump()?

0

All Articles