class Number (View source)

Methods

float|null
flexround(string $value)

Rounds the value only out to its most significant digit.

string
trim(string $value)

Remove superfluous zeroes from a string containing a number.

bool
isNumber($string)

Checks if a given string is valid representation of a number in the current locale.

bool
isInteger($string)

Checks if a given string is valid representation of an integer in the current locale.

string
format(number $number, int|null $precision = null)

Format a number with grouped thousands and localized decimal point/thousands separator.

null|number
unformat(string $string, bool $trim = true, int|null $precision = null)

Parses a localized number representation and returns the number (or null if $string is not a valid number representation).

string|mixed
formatSize(number $size, string $forceUnit = '')

Formats a size (measured in bytes, KB, MB, ...).

int|string
getBytes($val)

Nice and elegant function for converting memory. Thanks to @lightness races in orbit on Stackoverflow.

Details

float|null flexround(string $value)

Rounds the value only out to its most significant digit.

Parameters

string $value

Return Value

float|null

string trim(string $value)

Remove superfluous zeroes from a string containing a number.

Parameters

string $value

(decimal separator is dot)

Return Value

string

bool isNumber($string)

Checks if a given string is valid representation of a number in the current locale.

Parameters

$string

Return Value

bool

Examples

http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details

bool isInteger($string)

Checks if a given string is valid representation of an integer in the current locale.

Parameters

$string

Return Value

bool

Examples

http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details

string format(number $number, int|null $precision = null)

Format a number with grouped thousands and localized decimal point/thousands separator.

Parameters

number $number

The number being formatted

int|null $precision

[default: null] The wanted precision; if null or not specified the complete localized number will be returned

Return Value

string

Examples

http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details

null|number unformat(string $string, bool $trim = true, int|null $precision = null)

Parses a localized number representation and returns the number (or null if $string is not a valid number representation).

Parameters

string $string

The number representation to parse

bool $trim

[default: true] Remove spaces and new lines at the start/end of $string?

int|null $precision

[default: null] The wanted precision; if null or not specified the complete number will be returned

Return Value

null|number

Examples

http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details

string|mixed formatSize(number $size, string $forceUnit = '')

Formats a size (measured in bytes, KB, MB, ...).

Parameters

number $size

The size to be formatted, in bytes

string $forceUnit

= '' Set to 'bytes', 'KB', 'MB', 'GB' or 'TB' if you want to force the unit, leave empty to automatically determine the unit

Return Value

string|mixed

If $size is not numeric, the function returns $size (untouched), otherwise it returns the size with the correct usits (GB, MB, ...) and formatted following the locale rules

Examples

formatSize(0) returns '0 bytes'
formatSize(1) returns '1 byte'
formatSize(1000) returns '1,000 bytes'
formatSize(1024) returns '1.00 KB'
formatSize(1024, 'bytes') returns '1024 bytes'
formatSize(1024, 'GB') returns '0.00 GB'
formatSize(2000000) returns '1.91 MB'
formatSize(-5000) returns '-4.88 KB'
formatSize('hello') returns 'hello'

int|string getBytes($val)

Nice and elegant function for converting memory. Thanks to @lightness races in orbit on Stackoverflow.

Parameters

$val

Return Value

int|string