zeroise()
Add leading zeros when necessary.
If you set the threshold to '4' and the number is '10', then you will get back '0010'. If you set the threshold to '4' and the number is '5000', then you will get back '5000'.
Uses sprintf to append the amount of zeros based on the $threshold parameter and the size of the number. If the number is large enough, then no zeros will be appended.
No Hooks.
Return
String
. Adds leading zeros to number if needed.
Usage
zeroise( $number, $threshold );
- $number(int) (required)
- Number to append zeros to if not greater than threshold.
- $threshold(int) (required)
- Digit places number needs to be to not have zeros added.
Examples
#1 Basic Example
This code adds leading zeroes to the number of comments (must be used inside the comment loop). Here $threshold is 2, so leading zero will be added only to the one-digit numbers (1-9).
$comno = get_comments_number(); echo zeroise( $comno, 2 ); //> 01 02 03 ... 10 11 12 ...
Changelog
Since 0.71 | Introduced. |
zeroise() zeroise code WP 6.7.1
function zeroise( $number, $threshold ) { return sprintf( '%0' . $threshold . 's', $number ); }