bp_document_size_format( int|string $bytes, int $decimals )

Convert number of bytes largest unit bytes will fit into.

Description

It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts number of bytes to human readable number by taking the number of that unit that the bytes will go into it. Supports TB value.

Please note that integers in PHP are limited to 32 bits, unless they are on 64 bit architecture, then they have 64 bit size. If you need to place the larger size then what PHP integer type will hold, then use a string. It will be converted to a double, which should always have 64 bit length.

Technically the correct unit names for powers of 1024 are KiB, MiB etc.

Parameters

$bytes

(int|string) (Required) Number of bytes. Note max integer size for integers.

$decimals

(int) (Optional) Precision of number of decimal places. Default 0.

Return

(string|false) False on failure. Number string on success.

Source

File: bp-document/bp-document-functions.php

function bp_document_size_format( $bytes, $decimals = 0 ) {
	$quant = array(
		/* translators: Memory unit for terabyte. */
		_x( 'TB', 'memory unit', 'buddyboss' ) => TB_IN_BYTES,
		/* translators: Memory unit for gigabyte. */
		_x( 'GB', 'memory unit', 'buddyboss' ) => GB_IN_BYTES,
		/* translators: Memory unit for megabyte. */
		_x( 'MB', 'memory unit', 'buddyboss' ) => MB_IN_BYTES,
		/* translators: Memory unit for kilobyte. */
		_x( 'KB', 'memory unit', 'buddyboss' ) => KB_IN_BYTES,
		/* translators: Memory unit for byte. */
		_x( 'B', 'memory unit', 'buddyboss' )  => 1,
	);

	if ( 0 === $bytes ) {
		/* translators: Memory unit for byte. */
		return number_format_i18n( 0, $decimals ) . ' ' . _x( 'B', 'memory unit', 'buddyboss' );
	}

	foreach ( $quant as $unit => $mag ) {
		if ( doubleval( $bytes ) >= $mag ) {
			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
		}
	}

	return false;
}

Changelog

Changelog
Version Description
BuddyBoss 1.4.7 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.