dongyan4157 2014-07-24 01:49
浏览 11
已采纳

操作需要2位小数

I have this php code and i want this operations to apear with 2 decimals only. After the php code i'll put an example of what i receive on the output.

$Unir_Destino=$_POST['destino']; 
$tons=$_POST['tons'];
$descom=$_POST['descom'];
$remis=$_POST['remis'];
$fact=$_POST['fact'];
mysql_connect("localhost", "user", "password") or die (mysql_error()); 
mysql_select_db("Destinos") or die (mysql_error()); 
$Precio= mysql_query("SELECT Precio FROM Destinos WHERE Destino ='$Unir_Destino'");
$Precio1 = mysql_fetch_array($Precio);
$Precio2 = ($Precio1['Precio']);
$Total = $Precio2 * $tons;
$iva = ($Total / 100) * 16;
$totiva = $Total + $iva;
$rete = ($Total / 100) * 4;
$toret = $totiva - $rete;


        <span style="font-family: Tahoma;">
            <b>Subtotal:</b>       <?php print "$$Total <br />";?>
            <b>IVA:</b>            <?php print "$$iva <br />"; ?>
            <b>Subtotal + IVA:</b> <?php print "$$totiva <br />";?>
            <b>Retencion:</b>      <?php print "$$rete <br />";?>
            <b>Total:</b>          <?php print "$$toret <br />";?>
        </span>

Subtotal: $6297.9 IVA: $1007.664 Subtotal + IVA: $7305.564 Retencion: $251.916 Total: $7053.648

  • 写回答

1条回答 默认 最新

  • douhuantui6259 2014-07-24 01:52
    关注

    You can use PHP's number_format() method

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

    Here's an example taken from the reference:

    <?php
    
    $number = 1234.56;
    
    // english notation (default)
    $english_format_number = number_format($number);
    // 1,235
    
    // French notation
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56
    
    $number = 1234.5678;
    
    // english notation without thousands separator
    $english_format_number = number_format($number, 2, '.', '');
    // 1234.57
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角