gpt4 book ai didi

php number_format 给出错误信息

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:48 25 4
gpt4 key购买 nike

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

警告:number_format() 期望参数 1 为 double ,对象在/home/chesspro/public_html/UNYCL/people-8.php 第 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>';}
?>

现在我可以解决这个问题吗?我需要一点小数精度 (X.X) 虽然有些数字是整数,如“7”,我想将其格式化并显示为“7.0” 请注意,我使用的是接近当前的 php 版本 5.3.4

最佳答案

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

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

如果这不起作用尝试类型转换:http://us3.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

$X = (float)$game->white->score;
print '<td>'. number_format($X) ."</td>";

关于php number_format 给出错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8628498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com