Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php number_格式给出错误消息_Php_Xml - Fatal编程技术网

php number_格式给出错误消息

php number_格式给出错误消息,php,xml,Php,Xml,我正在使用php解析XML文件,但在尝试显示格式化数字时收到错误消息: 警告:number_format()要求参数1为双精度,第45行的/home/chesspro/public_html/UNYCL/people-8.php中给出的对象 <?php $xml = simplexml_load_file('Rochester_1.xml'); foreach($xml->meet as $item) { print '<table class="basic re

我正在使用php解析XML文件,但在尝试显示格式化数字时收到错误消息:

警告:number_format()要求参数1为双精度,第45行的/home/chesspro/public_html/UNYCL/people-8.php中给出的对象

<?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>';}
?>

现在我能修好这个吗?我需要一个小数点精度(X.X),尽管有些数字是像“7”这样的整数,我想格式化并显示为“7.0”。请注意,我使用的是接近当前的php版本5.3.4。数字格式($X,1)。“”;
print '<td>'. number_format($X, 1) ."</td>";

如果这不起作用,请尝试类型转换:

$X=(浮动)$game->white->score;
打印“”。数字格式($X)。“”;

它看起来像
$game->white->score
不返回float或int。 试着这样做
$X=(浮动)$game->white->score;
打印“”。数字格式($X)。“”


但是带有一个参数的number_format()将返回以千为单位分组的数字,所以如果您想要像7.0一样,最好使用
print'。数字格式($X,1)。“”

当您将其更改为:
$subtotal=$subtotal+floatval((字符串)$temp->white->score)时,它是否有效?绝对完美!结果是:谢谢你们!警告:浮子是危险的。例:与11不同,向浮子浇铸可能会产生10.99999999999
$X = (float)$game->white->score;
print '<td>'. number_format($X) ."</td>";