dslijian2015 2018-04-20 09:55
浏览 63

格式化数字在PHP中可能带有前导零

I'm having a hard time to figure out, how to format a range of number correctly.

Lets say I have an array with the following values:

array( 1, 2, 001, 02, 012 );

How can I change the output of each value to fit the following format?

0 => 1

1 => 2

2 => 0.01

3 => 0.2

4 => 0.12

I'm not looking for a loop ( this is just for explanation), just a way to format the number correctly.

  • 写回答

1条回答 默认 最新

  • douhengdao4499 2018-04-20 10:15
    关注

    This is the way PHP interpret Integer you can see you have no leading zeros on Integer ;-)

    var_dump(array( 1, 2, 001, 02, 012 ));
    
    array(5) {
      [0]=>int(1)
      [1]=>int(2)
      [2]=>int(1)
      [3]=>int(2)
      [4]=>int(10)
    }
    

    The only way to solve it is converting it to a String.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?