dsgd5756 2016-05-23 17:39
浏览 54
已采纳

在乘法php中舍入2位小数不是舍入

I come to you looking for answers, I have a table and I am calculating the tax field multiplying 1 variable per 0.16 but I want it to have just 2 decimals, not 10 or 4 or 3, I was trying with round() and with number_format() but is not working, there must be something I am doing wrong, how it suppose that I need to do it ?

I am showing the code

    $html .= '<tr>
    <td>'.escape($r->tecnologia).'</td>
    <td>'.escape($r->normaespecificacion).'</td>
    <td><img src="functions/'.escape($r->foto).'"></td>
    <td>'.escape($r->marca).'</td>
    <td>'.escape($r->modelo).'</td>  
    <td>'.escape($r->descripcion).'</td>
    <td>$'.escape($r->preciounitario).'</td>
    <td>$'.escape($r->number_format(preciounitario*0.16,2)).'</td> //this is the one with the error
    <td>$'.escape($r->instalacion).'</td>
    <td>$'.escape($r->totalcosto).'</td>
    <td>'.escape($r->garantia).'</td>
    <td>'.$listNum[$i].'</td>
    </tr>';
    $totalV += $listNum[$i]*$r->totalcosto;
    $i = $i+1;
}
  • 写回答

1条回答 默认 最新

  • dtv995719 2016-05-23 17:51
    关注

    If you are just looking to round a number, then here is the solution:

    <?php
        $number = 1353.234242;
        $rounded = (float)sprintf('%.2f', $number);
        echo $rounded; // will display 1353.23
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加