首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法用php代码生成2位小数的金额

无法用php代码生成2位小数的金额
EN

Stack Overflow用户
提问于 2018-06-16 03:41:18
回答 3查看 87关注 0票数 1

我是否需要以不同的方式定义我的变量$amount。我很难让它以这种方式显示为IMAGE

这就是给所有的东西加2个零。我错过了什么。

我想看看我是否可以把美元符号也包括在这张图片中。不确定这是额外的代码,还是需要添加的函数。

代码语言:javascript
复制
<?php  
$amount = '';
    echo number_format(floor($amount*100)/100,2, '.', ''); 
 ?>
 <html>
 <head>
    <title>Display Transaction Table</title>

<script>

</script>
</head>

 <body>
 <h1>Transactions</h1>
 <div id="myTable">
 <input class="search" placeholder="Search" /><!--Not Required, but useful-->

<table class="data-table" action="transactions.php" id ="myTable">

    <thead>
        <tr>
            <th class="sort" data-sort="id">ID</th>
            <th class="sort" data-sort="fund">Fund</th>
            <th class="sort" data-sort="department">Department</th>
            <th class="sort" data-sort="code">Code</th>
            <th class="sort" data-sort="year">Year</th>
            <th class="sort" data-sort="date">Date</th>
            <th class="sort" data-sort="project">Project</th>
            <th class="sort" data-sort="description">Description</th>
            <th class="sort" data-sort="amount">Amount</th>
            <th class="sort" data-sort="detail">Detail</th>
            <th class="sort" data-sort="po">PO</th>
            <th class="sort" data-sort="type">Type</th>

        </tr>
    </thead>
    <tbody class="list">
    <?php
    $transaction_id = 1;


    while ($row = sqlsrv_fetch_array($query))
     {
    $amount  = $row['amount'] == 0 ? '' : number_format($row['amount']);

        echo '<tr>
                <td class="id">'.$transaction_id.'</td>
                <td class="fund">'.$row['fund'].'</td>
                <td class="department">'.$row['department'].'</td>
                <td class="code">'.$row['code_name'].'</td>
                <td class="year">'.$row['budget_year'].'</td>
                <td class="date">'.$row['entry_date'].'</td>
                <td class="project">'.$row['project_name'].'</td>
                <td class="description">'.$row['item_desc'].'</td>
                <td class="amount">'.$row['amount'].'</td>
                <td class="detail">'.$row['detail'].'</td>
                <td class="po">'.$row['PO'].'</td>
                <td class="type">'.$row['type'].'</td>
                <td><a href="edittest.php?edit='.$row["transaction_id"].'">Edit</a></td>  
            </tr>';


        $transaction_id++;
    }?>


    </tbody>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-16 03:46:49

您的问题是您没有在表中使用$amount。您正在使用$row['amount']

代码语言:javascript
复制
// change this
<td class="amount">'.$row['amount'].'</td>

// to this
<td class="amount">'.$amount.'</td>
票数 1
EN

Stack Overflow用户

发布于 2018-06-16 03:54:52

您可以修改$amount计算,如下所示:

代码语言:javascript
复制
$amount = (!empty($row['amount'])) ? number_format($row['amount'], 2) : 0;

并更改金额行:

代码语言:javascript
复制
'<td class="amount">'.$row['amount'].'</td>'

代码语言:javascript
复制
'<td class="amount">'. $amount .'</td>'

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2018-06-16 04:08:23

更改代码

代码语言:javascript
复制
 $amount  = $row['amount'] == 0 ? '' : number_format($row['amount']);

代码语言:javascript
复制
$amount  = $row['amount'] == 0 ? '' : number_format($row['amount'],2);

这已经更新了我的表!!谢谢大家

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50881769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档