zeroise

Add leading zeros when necessary.

Description

(string) zeroise( (number) $number, (int) $threshold ); 

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.

Returns (string)

Adds leading zeros to number if needed.

Parameters (2)

0. $number (number)
Number to append zeros to if not greater than threshold.
1. $threshold (int)
Digit places number needs to be to not have zeros added.

Usage

  1. if ( !function_exists( 'zeroise' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Number to append zeros to if not greater than threshold. 
  5. $number = null; 
  6.  
  7. // Digit places number needs to be to not have zeros added. 
  8. $threshold = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = zeroise($number, $threshold); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function zeroise( $number, $threshold ) { 
  2. return sprintf( '%0' . $threshold . 's', $number );