简体   繁体   English

如何阻止PHP舍入我的条形码数字

[英]How to stop PHP from rounding my barcode numbers

I have some bar code numbers in an array. 我在数组中有一些条形码。 PHP seems to be rounding the barcodes which start with leading zeros. PHP似乎是围绕以前导零开头的条形码。 How do I stop this happening and keep the numbers as they were? 我如何阻止这种情况发生并保持数字原样? Code I am using is below: 我正在使用的代码如下:

$array = array(5032227448124,5060028999989,5010121096504,5060028999996,5016254104864,5016402052788,8422248036986,0000003798720,0000003735503,0000003798713);

echo '<pre>';
print_r($array);
echo '</pre>';

This echos the following, as you can see the last four bar codes which feature leading zeros have been changed and had their leading zeros removed. 这回事如下,正如您所看到的那样,最后四个以前导零为特征的条形码已被更改并删除了前导零。 These numbers are always 13 digits long and are padded with zeros. 这些数字总是13位数字,并用零填充。

Array
(
    [0] => 5032227448124
    [1] => 5060028999989
    [2] => 5010121096504
    [3] => 5060028999996
    [4] => 5016254104864
    [5] => 5016402052788
    [6] => 8422248036986
    [7] => 31
    [8] => 1030979
    [9] => 31
    [10] => 1031004
)

如果它们不是数字(整数,浮点数,指数),则需要将它们作为字符串引用。

  1. The obvious, easy, and also likely wrong answer is to make them strings. 明显的,简单的,也可能是错误的答案是使它们成为字符串。
  2. The better answer is to use printf() / sprintf() to pad with zeroes: 更好的答案是使用printf() / sprintf()来填充零:

     printf('%013d', 12345); // output: 0000000012345 
  3. MySQL also has a handy LPAD() function: MySQL还有一个方便的LPAD()函数:

     SELECT LPAD(12345, 13, 0) // output 0000000012345 

这是将值转换为填充字符串的简单方法:

$array = array_map(function ($e){return str_pad($e, 13, "0", STR_PAD_LEFT);}, $array);

最后,我只需要在每个条形码编号周围加上双引号,例如

 $array = array("5032227448124","5060028999989","5010121096504","5060028999996","5016254104864","5016402052788","8422248036986","0000003798720","0000003735503","0000003798713");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM