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
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>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With