douxi2011 2013-10-20 00:57
浏览 39
已采纳

格式化PHP货币值[重复]

This question already has an answer here:

I'm developing a browser extension (content script) that scans and highlights certain words on a page, and uses AJAX and PHP to echo back content into a tooltip that appears when a user hover over said words. The PHP file uses an API to retrieve an XML file which it then parses and echoes back.

One thing that gets echoed into the tooltip is a money value (the 'total' attribute in the code below). As of now, it spits back values like "$612343", $121100", "$1222" etc. What I want is for the money to be formatted properly with commas like "$6,123,234" instead of "$6123234". How would I do this?

Here is the PHP code:

echo "<table><tr id='thasdfdfd'><th id='thasdfdfd'>Contributors</th><th>Total</th></tr>";
foreach ($data['response']['industries']['industry'] as $ind) {
    foreach ($ind as $row) {
        echo "<tr><td>" . $row['industry_name'] . " " . "</td><td>" . "$" . 
            $row['total'] . '</td></tr>';
    }
}
</div>
  • 写回答

2条回答 默认 最新

  • dpxua26604 2013-10-20 01:03
    关注

    You probably want something like number_format. It lets you specify the thousands separator, decimal places, and decimal separator. Just the normal, single argument usage will probably yield what you want.

    <?php
    echo number_format('612343'), PHP_EOL;
    // output: 612,343
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分