使用php代码无法使用2位小数制作金额

时间:2018-06-15 19:41:18

标签: php

我是否需要以不同的方式定义变量$amount。我很难尝试将其显示为IMAGE

它为所有东西增加了2个零。我错过了什么

我想知道我是否可以获得此图片中包含的美元符号。不确定这是否是额外的代码,或者是否是需要添加的函数。

<?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>

3 个答案:

答案 0 :(得分:1)

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

// change this
<td class="amount">'.$row['amount'].'</td>

// to this
<td class="amount">'.$amount.'</td>

答案 1 :(得分:0)

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

$amount = (!empty($row['amount'])) ? number_format($row['amount'], 2) : 0;

并更改金额行:

'<td class="amount">'.$row['amount'].'</td>'

'<td class="amount">'. $amount .'</td>'

希望这有帮助。

答案 2 :(得分:0)

更改代码
 $amount  = $row['amount'] == 0 ? '' : number_format($row['amount']);

$amount  = $row['amount'] == 0 ? '' : number_format($row['amount'],2);

这更新了我的桌子!! 谢谢大家