【问题标题】:Subtract string and convert to decimal without rounding减去字符串并转换为十进制而不四舍五入
【发布时间】:2019-11-22 09:03:41
【问题描述】:

我有一个数字声明

$barcode_scan = '220008100383005193';

我想要这样的输出0.519

我已经尝试过这段代码

number_format(substr($barcode_scan, -6), 0, '', '.');

但输出是5.193。有什么帮助吗?谢谢

【问题讨论】:

  • 为什么要脱掉'2200081003830',标准是什么?
  • 这看起来非常像this 其他问题。
  • 我不明白你的问题,但这是你想要的吗? 3v4l.org/HLpqi
  • 因为 $barcode_scan 是来自产品的条形码。 $barcode_scan 的最后六个实际上是产品的重量。这就是为什么我想取消另一个号码

标签: php


【解决方案1】:

类似的东西是here

在你的情况下,代码应该是下一个:

$barcode_scan = '220008100383005193';
echo floor(number_format(substr($barcode_scan, -6), 0, '', '.')*100)/1000;

Outputs:

0.519

【讨论】:

    【解决方案2】:

    试试下面的代码

    number_format(substr($barcode_scan, -6)/10000, 3)

    number_format(substr($barcode_scan, -6)/10000, 3, '.', ',')

    即小数点符号和千位分隔符冲突

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多