Storing numbers with zeroise function
Today i want to introduce you a small formatting function included in WordPress that will help you store in your database numbers that need to be formatted with the exact same number of figures. Let’s say for example you need to store numbers that must contain 4 figures. If a number is smaller than 1000, its format won’t match your need (only 3 figures). That’s what zeroise() is made for, it will add leading zeros when necessary.
Zeroise ? What’s that for ?
In our example 1000 contains 4 figures so that’s ok, but if the value you have to store is, let’s say, 452, it only contains 3 figures. Using zeroise() will simple add a zero before the value. That’s to say: 0452. That’s all that simple ! All numbers that don’t match your arguments will be formatted. The zeroise() function contains 2 parameters:
- $number : Number to append zeros to if not greater than threshold
- $threshold : Digit places number needs to be to not have zeros added
Just to make sure you understood the function goals: what will be outputted if you set zeroise( 12, 5) ?
00012 correct !