0

I want to zerofill a number so that the resulting nunber always has two chars.

For example: 1.7 -> 01.7 34.7 -> 34.6 // Remains the same, already two chars

I tried using printf('%05.1f', 12.2); but this always adds a char to the number no matter what. I only want that to happen when it has only one char (< 10). Also decimals should not be removed.

qwertz
  • 6,206
  • 9
  • 40
  • 62

2 Answers2

1

You should write

printf('%04.1f', 12.2)

because you want your numbers to have at least four characters (including the .)

You may want to check out this: http://alvinalexander.com/programming/printf-format-cheat-sheet#printf_-_floating_point_numbers

mwoelk
  • 1,182
  • 10
  • 14
0

You can use if else for that

if($num <10){
  printf('%05.1f', 12.2);
}
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78