zeroise() WordPress Function

The zeroise() function is used to add leading zeros to a number. It is often used when formatting numbers for display, such as when displaying a four-digit year. The zeroise() function takes two arguments: the number to be formatted and the desired length of the formatted number. The function returns the formatted number as a string.

zeroise( int $number, int $threshold ) #

Add leading zeros when necessary.


Description

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.


Top ↑

Parameters

$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.


Top ↑

Return

(string) Adds leading zeros to number if needed.


Top ↑

Source

File: wp-includes/formatting.php

function zeroise( $number, $threshold ) {
	return sprintf( '%0' . $threshold . 's', $number );
}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More