Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php number_format gives error message

Tags:

php

xml

I am using php to parse an XML file, but I get an error message when attempting to display a formatted number:

Warning: number_format() expects parameter 1 to be double, object given in /home/chesspro/public_html/UNYCL/people-8.php on line 45

<?php 
$xml = simplexml_load_file('Rochester_1.xml');

foreach($xml->meet as $item) {

    print '<table class="basic report tableland brown_gradient"><thead>';
    print '<tr><th class="R-align">Board</th><th class="L-align">'.$item->visitor.'</th>';

    $subtotal = 0;
    foreach($item->match as $temp) {$subtotal = $subtotal + $temp->white->score;}
    print "<th>" .number_format($subtotal,1). "</th>";

    $subtotal = 0;
    foreach($item->match as $temp) {$subtotal = $subtotal + $temp->black->score;}
    print "<th>" .number_format($subtotal,1). "</th>";

    print '<th class="L-align">'.$item->home.'</th>';

    print '</tr><tbody>';

    foreach($item->match as $game) {

    print '<tr><td class="R-align">'. $game->board . "</td>";
    print '<td>'. $game->white->player."</td>";

    //Here is error: note that number is sometimes 0
        $X = $game->white->score;
        print '<td>'. number_format($X) ."</td>";

    print '<td>'. $game->black->score."</td>";       
    print '<td class="L-align">'. $game->black->player."</td>";

    print "</tr>";}
print '</table>';}
?>

Now can I fix this? I need one point of decimal precision (X.X) although some numbers are integers like "7" which I want to format and display as "7.0" Note that I am using a near current php version 5.3.4

like image 253
verlager Avatar asked Mar 17 '26 14:03

verlager


1 Answers

print '<td>'. number_format($X, 1) ."</td>";

See http://php.net/manual/en/function.number-format.php

If this doesn't work try type casting: https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

$X = (float)$game->white->score;
print '<td>'. number_format($X) ."</td>";
like image 55
Andrew Samuelsen Avatar answered Mar 19 '26 04:03

Andrew Samuelsen