MG1
MG1

Reputation: 1687

php only adding the first digit instead of entire number

I recently moved my website over to Blue Host and this function that's part of a loop:

if ($InvoiceType == '18')
{
    echo 'Sum Before addition = ' . $sumInvoice . '<br/>';
    $sumInvoice += $complete[$key][5];
    print 'Invoice being added = ' . $complete[$key][5] . '<br/>' 
        . 'Sum after addition = '. $sumInvoice . '<br/>';
}       

Is returning this:

Sum Before addition = 0
Invoice being added = 15174.20
Sum after addition = 1
Sum Before addition = 1
Invoice being added = 4604.25
Sum after addition = 5
Sum Before addition = 5
Invoice being added = 63810.83
Sum after addition = 11

dump of $complete:

Array
(
    [0] => Array
    (
        [0] => 2161
        [1] => 638697
        [2] => CLIV0286
        [3] => Central Medical Supply Inc.
        [4] => 0.00
        [5] => 15174.20
        [6] => 04/01/2011
        [7] => 2/1-2/28/11 Fairlawn
        [8] => PM2
        [9] => 18
    )
    [1] => Array
    (
        [0] => 2161
        [1] => 638698
        [2] => CLIV0286
        [3] => Central Medical Supply Inc.
        [4] => 0.00
        [5] => 4604.25
        [6] => 04/01/2011
        [7] => 2/1-2/28/11 Atrium
        [8] => PM2
        [9] => 18
    )
    [2] => Array
        (
        [0] => 2161
        [1] => 638699
        [2] => CLIV0286
        [3] => Central Medical Supply Inc.
        [4] => 0.00
        [5] => 63810.83
        [6] => 04/01/2011
        [7] => 2/1-2/28/11 Parsippany
        [8] => PM2
        [9] => 18
    )
)

What am I doing wrong?

Upvotes: 0

Views: 378

Answers (2)

Craig White
Craig White

Reputation: 14012

So this does not work?

$sumInvoice = 0;
//Init Loop
if ($InvoiceType == '18')
{
    echo 'Sum Before addition = ' . $sumInvoice . '<br/>';
    $sumInvoice = 0 + floatval(trim($complete[$key][5])) + floatval($sumInvoice);
    print 'Invoice being added = ' . $complete[$key][5] . '<br/>' 
        . 'Sum after addition = '. $sumInvoice . '<br/>';
}

Try this small scale test:

$val = "10.25";
echo (floatval($val) + 10);

Upvotes: 1

bensiu
bensiu

Reputation: 25604

$sumInvoice += (float)$complete[$key][5];

or

$sumInvoice += floatval( $complete[$key][5] );

floatval()

Upvotes: 0

Related Questions