1
2There are already some comments on using sprintf to force leading leading zeros but the examples only include integers. I needed leading zeros on floating point numbers and was surprised that it didn't work as expected.
3
4Example:
5<?php
6sprintf('%02d', 1);
7?>
8
9This will result in 01. However, trying the same for a float with precision doesn't work:
10
11<?php
12sprintf('%02.2f', 1);
13?>
14
15Yields 1.00.
16
17This threw me a little off. To get the desired result, one needs to add the precision (2) and the length of the decimal seperator "." (1). So the correct pattern would be
18
19<?php
20sprintf('%05.2f', 1);
21?>
22
23Output: 01.00
24
25Please see http://stackoverflow.com/a/28739819/413531 for a more detailed explanation.
26