Numeric helper class.

Provides additional formatting methods for working with numeric values.

Credit is left where credit is due.

Techniques and inspiration were taken from all over, including: Kohana Framework: kohanaframework.org CakePHP: cakephp.org

package Fuel
category Core
author Chase "Syntaqx" Hutchins

 Methods

Class initialization callback

_init() : void

Converts a file size number to a byte value.

bytes(string $size) : float

File sizes are defined in the format: SB, where S is the size (1, 8.5, 300, etc.) and B is the byte unit (K, MiB, GB, etc.). All valid byte units are defined in static::$byte_units

Usage:

echo Num::bytes('200K');  // 204800
echo static::bytes('5MiB');  // 5242880
echo static::bytes('1000');  // 1000
echo static::bytes('2.5GB'); // 2684354560

author Kohana Team
copyright (c) 2009-2011 Kohana Team
license http://kohanaframework.org/license

Parameters

$size

string

file size in SB format

Returns

float

Formats a number by injecting non-numeric characters in a specified format into the string in the positions they appear in the format.

format(string $string, string $format) : string

Usage:

echo Num::format('1234567890', '(000) 000-0000'); // (123) 456-7890
echo Num::format('1234567890', '000.000.0000'); // 123.456.7890

link http://snippets.symfony-project.org/snippet/157

Parameters

$string

string

the string to format

$format

string

the format to apply

Returns

string

Converts a number of bytes to a human readable number by taking the number of that unit that the bytes will go into it.

format_bytes($bytes, $decimals) : boolean | string

Supports TB value.

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

Parameters

$bytes

integer

$decimals

integer

Returns

booleanstring

Formats a credit card expiration string.

format_exp(string $string, string $format) 

Expects 4-digit string (MMYY).

see \Fuel\Core\format

Parameters

$string

string

the unformatted expiration string to format

$format

string

the format to use, defaults to '00-00'

Formats a phone number.

format_phone(string $string, string $format) : string

link http://snippets.symfony-project.org/snippet/157
see \Fuel\Core\format

Parameters

$string

string

the unformatted phone number to format

$format

string

the format to use, defaults to '(000) 000-0000'

Returns

stringthe formatted string

Formats (masks) a credit card.

mask_credit_card(string $string, string $format) 

see \Fuel\Core\mask_string

Parameters

$string

string

the unformatted credit card number to format

$format

string

the format to use, defaults to '**** **** **** 0000'

Transforms a number by masking characters in a specified mask format, and ignoring characters that should be injected into the string without matching a character from the original string (defaults to space).

mask_string(string $string, string $format, string $ignore) : string

Usage:

echo Num::mask_string('1234567812345678', '************0000'); ************5678
echo Num::mask_string('1234567812345678', '**** **** **** 0000'); // **** **** **** 5678
echo Num::mask_string('1234567812345678', '**** - **** - **** - 0000', ' -'); // **** - **** - **** - 5678

link http://snippets.symfony-project.org/snippet/157

Parameters

$string

string

the string to transform

$format

string

the mask format

$ignore

string

a string (defaults to a single space) containing characters to ignore in the format

Returns

stringthe masked string

Converts a number into a more readable human-type number.

quantity($num, $decimals) : string

Usage:

echo Num::quantity(7000); // 7K
echo Num::quantity(7500); // 8K
echo Num::quantity(7500, 1); // 7.5K

Parameters

$num

integer

$decimals

integer

Returns

string

Formats a variable length phone number, using a standard format.

smart_format_phone(string $string) 

Usage:

echo Num::smart_format_phone('1234567'); // 123-4567
echo Num::smart_format_phone('1234567890'); // (123) 456-7890
echo Num::smart_format_phone('91234567890'); // 9 (123) 456-7890
echo Num::smart_format_phone('123456'); // => 123456

see \Fuel\Core\format

Parameters

$string

string

the unformatted phone number to format

 Properties

 

Cached byte units

$byte_units : array

 

Cached configuration values

$config : array