【问题标题】:How to format a decimal number based on certain values in php如何根据php中的某些值格式化十进制数
【发布时间】:2026-01-09 06:10:01
【问题描述】:

我的价值观很少 99.00 99.90 99.01

仅供参考,在对它们应用 number_format($value, 2) 后,我已经获得了上述 3 个值

但现在我想去掉小数而不四舍五入,例如 99.00 至 99 99.90 至 99.9 99.01 到 99.01 保持不变

如何做到这一点?伙计们,请给我一个办法。

我只需要一个检查以下内容的函数:

  1. 给定数字是否为十进制,即是否有“.”和小数点后的数字
  2. 最后一位是否为0,如果是,则去掉最后一位
  3. 小数点后两位数是否为0,如果是,删除。
  4. 如果其中任何一个不为 0,那么它们应该保留在那里,就像在 99.09 的情况下它应该保持不变,在 99.90 的情况下它应该是 99.9。

等待您的想法。提前致谢。

【问题讨论】:

  • round()代替number_format()?
  • 如果我的回答解决了您的问题,请将我的回答标记为已接受 :)
  • 艰难的选择,有三个不错的答案和方法;)
  • 我更新了我的。

标签: php decimal number-formatting


【解决方案1】:

通过将0 添加到数字,最右边的零被删除:

$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;

$num1 = number_format(99.00,   2, '.', '') + 0;
$num2 = number_format(99.90,   2, '.', '') + 0;
$num3 = number_format(99.01,   2, '.', '') + 0;
$num4 = number_format(99.012,  2, '.', '') + 0;
$num5 = number_format(99.0123, 2, '.', '') + 0;

echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";

输出:

99
99.9
99.01
99.01
99.01

试试here

function

function round2no0(&$num)
{
    $num = number_format($num, 2, '.', '') + 0;
}

用法:

$num1 = 99.00;
$num2 = 99.90;
$num3 = 99.01;
$num4 = 99.012;
$num5 = 99.0123;

round2no0($num1);
round2no0($num2);
round2no0($num3);
round2no0($num4);
round2no0($num5);

echo "$num1\n";
echo "$num2\n";
echo "$num3\n";
echo "$num4\n";
echo "$num5\n";

function round2no0(&$num)
{
    $num = number_format($num, 2, '.', '') + 0;
}

输出:

99
99.9
99.01
99.01
99.01

编辑:

, '.', '' 参数添加到number_format 以处理数千个保持机器格式12345.12 的数字。

试试here

【讨论】:

  • 只有当期望的结果是最多 2 位小数时,您的示例才是正确的。
  • @PramodKharade,你在这里评论的目的?!?
  • 我尝试了上述方法,但某些值存在问题,例如 1 1 99.11 99 99 请注意 num1 和 num2 返回为 1 而我需要它为 1,200
  • @Asnexplore 看到我的编辑:现在它也可以正确处理带有 tousands 的数字。
【解决方案2】:

如果您对number_format 使用自定义表示法,则可以使用preg_replace 删除小数点分隔符和其后的尾随零。

// Default notation
echo preg_replace('/\.?0+$/', '', number_format(9990.00, 2)); // "9,990"
echo preg_replace('/\.?0+$/', '', number_format(9990.90, 2)); // "9,990.9"
echo preg_replace('/\.?0+$/', '', number_format(9990.01, 2)); // "9,990.01"

// French notation
echo preg_replace('/,?0+$/', '', number_format(9990.00, 2, ',', ' ')); // "9 990"
echo preg_replace('/,?0+$/', '', number_format(9990.90, 2, ',', ' ')); // "9 990,9"
echo preg_replace('/,?0+$/', '', number_format(9990.01, 2, ',', ' ')); // "9 990,01"

【讨论】:

    【解决方案3】:

    试试这个:

    echo round(99.001, 2), PHP_EOL;
    echo round(99.901, 2), PHP_EOL;
    echo round(99.011, 2), PHP_EOL;
    

    输出:

    99
    99.9
    99.01
    

    【讨论】:

    • number_format 也进行了舍入,所以我认为 Asnexplore 可以进行舍入。你的答案是最干净的解决方案(他只需要这个,仅此而已),不知道为什么要投反对票。
    • @IncredibleHat 你说得对,他确实已经进行了四舍五入,所以我删除了我添加的额外内容。
    • 是的,四舍五入等都已完成。我需要这些结果来清理尾随的 0 或 0。
    • 尝试使用 1200.91、1,200.00、1,200.90、1,200 等数字。如果逗号不存在,它们将显示为 1,但使用逗号它们会生成错误消息 Parse error: syntax error, unexpected ',' in /tmp/execpad-edf220d84a3a/source-edf220d84a3a on line 2
    • 虽然我很感激你的辛勤工作帮助我,但不幸的是,它仍然没有达到我想要的效果。
    【解决方案4】:

    您应该可以将其包装在 floatval()

    【讨论】:

      最近更新 更多