php number_format给出错误消息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php number_format给出错误消息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1611字,纯文字阅读大概需要3分钟。
内容图文
![php number_format给出错误消息](/upload/InfoBanner/zyjiaocheng/655/70e260ed0797443a9a208f7a7dfdfd2a.jpg)
我使用php解析XML文件,但是尝试显示格式化的数字时收到错误消息:
警告:number_format()期望参数1为double,第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
解决方法:
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给出错误消息全部内容,希望文章能够帮你解决php number_format给出错误消息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。