duan1933 2013-05-06 16:30
浏览 46
已采纳

使用PHP格式化数字

I'm saving numbers in MySQL using double(10,2).

The numbers are getting saved like:

456.2

232.20

764

On output, I want to force 2 decimals and add the necessary trailing zeros (currency).

Here is the function I'm using and it isn't working.

function format_currency_form($get_money) {

    $money_output = abs(number_format($get_money, 2, '.', ''));

    return $money_output;
}

(I'm using ABS because I want the value to display as a positive number regardless of its true value in the db)

Here is the code block used to output the data:

<?php echo format_currency_form($row_rs_data['trans_amount']); ?>

No errors getting thrown and no trailing zeros...

Any ideas?

Thanks

Brett

  • 写回答

2条回答 默认 最新

  • dpbdl44228 2013-05-06 16:34
    关注

    If you are dealing with currency try to use moneyformat

    http://php.net/manual/en/function.money-format.php

    money_format('%.2n', $number)

    In your case the abs should be done in this way:

    $number=-11.44;
    $money_output = money_format('%.2n', abs($number));
    echo $money_output;
    

    If you are dealing with currencies (I think so) money_format gives you a lot of options.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大