robtillaart:
The code works for numbers within the rang 0..999try the code with a number that is longer or a negative and you see some point to improve the function.
Quite right. I had meant to point that out in my reply.
In practice I would probably not use such a function in my code as it only replaces two lines with one in the main code and I would feel more in control of the formatting by having the sprintf on the line before the print instead of hidden in a function. It would also become more and more difficult to extend the function to handle other formats properly.