Flourish PHP Unframework

fNumber

class, v1.0.0b3

Provides large/precise number support

Changes:
1.0.0b3Added the $remove_zero_fraction parameter to format() 2/2/11
1.0.0b2Fixed a bug with parsing decimal numbers in scientific notation 4/13/10
1.0.0bThe initial implementation 7/21/08

Static Methods

::baseConvert() public

Converts any positive integer between any two bases ranging from 2 to 16

Signature

string baseConvert( fNumber|string $number, integer $from_base, integer $to_base )

Parameters

fNumber|string $number The positive integer to convert
integer $from_base The base to convert from - must be between 2 and 16
integer $to_base The base to convert to - must be between 2 and 16

Returns

The number converted to the new base

::pi() public

Returns the value for pi with a scale of up to 500

Signature

fNumber pi( integer $scale )

Parameters

integer $scale The number of places after the decimal to return

Returns

Pi

::registerFormatCallback() public

Allows setting a callback to translate or modify any return values from format()

The callback should accept two parameters:

  • $value: the string value of the number
  • $remove_zero_fraction: a boolean indicating if a zero fraction should be removed

The callback should return a string, the formatted $value.

Signature

void registerFormatCallback( callback $callback )

Parameters

callback $callback The callback to pass the fNumber value to - see method description for parameters

::registerUnformatCallback() public

Allows setting a callback to clean any formatted values so they can be properly parsed - useful for languages where , is used as the decimal point

Signature

void registerUnformatCallback( callback $callback )

Parameters

callback $callback The callback to pass formatted strings to. Should accept a formatted string and return a string the is a valid number using . as the decimal point.

::reset() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Resets the configuration of the class

Signature

void reset( )

Methods

->__construct() public

Creates a large/precise number

Signature

fNumber __construct( string $value, integer $scale=NULL )

Parameters

string $value The value for the number - any valid PHP integer or float format including values with e exponents
integer $scale The number of digits after the decimal place, defaults to number of digits in $value

Throws

fValidationException
When $value is not a valid number

->__get() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

All requests that hit this method should be requests for callbacks

Signature

callback __get( string $method )

Parameters

string $method The method to create a callback for

Returns

The callback for the method requested

->__toString() public

Converts the object to an string

Signature

string __toString( )

->abs() public

Returns the absolute value of this number

Signature

fNumber abs( integer $scale=NULL )

Parameters

integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The absolute number

->add() public

Adds two numbers together

Signature

fNumber add( fNumber|string $addend, integer $scale=NULL )

Parameters

fNumber|string $addend The addend
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The sum

Throws

fValidationException
When $addend is not a valid number

->ceil() public

Rounds the number to the next highest integer

Signature

fNumber ceil( )

Returns

The next highest integer

->div() public

Divides this number by the one passed

Signature

fNumber div( fNumber|string $divisor, integer $scale=NULL )

Parameters

fNumber|string $divisor The divisor
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The quotient

Throws

fValidationException
When $divisor is not a valid number

->eq() public

Indicates if this value is equal to the one passed

Signature

boolean eq( fNumber|string $number, integer $scale=NULL )

Parameters

fNumber|string $number The number to compare to
integer $scale The number of decimal places to compare - will use all available if not specified

Returns

If this number is equal to the one passed

Throws

fValidationException
When $number is not a valid number

->floor() public

Rounds the number to the next lowest integer

Signature

fNumber floor( )

Returns

The next lowest integer

->fmod() public

Returns the float remainder of dividing this number by the divisor provided

Signature

fNumber fmod( fNumber|string $divisor, integer $scale=NULL )

Parameters

fNumber|string $divisor The divisor
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The remainder

Throws

fValidationException
When $divisor is not a valid number

->format() public

Formats the number to include thousands separators

Signature

string format( boolean $remove_zero_fraction=FALSE )

Parameters

boolean $remove_zero_fraction If TRUE and all digits after the decimal place are 0, the decimal place and all zeros are removed

Returns

The formatted value

->getScale() public

Returns the scale of this number

Signature

integer getScale( )

Returns

The scale of this number

->gt() public

Indicates if this value is greater than the one passed

Signature

boolean gt( fNumber|string $number, integer $scale=NULL )

Parameters

fNumber|string $number The number to compare to
integer $scale The number of decimal places to compare - will use all available if not specified

Returns

If this number is less than the one passed

Throws

fValidationException
When $number is not a valid number

->gte() public

Indicates if this value is greater than or equal to the one passed

Signature

boolean gte( fNumber|string $number, integer $scale=NULL )

Parameters

fNumber|string $number The number to compare to
integer $scale The number of decimal places to compare - will use all available if not specified

Returns

If this number is greater than or equal to the one passed

Throws

fValidationException
When $number is not a valid number

->lt() public

Indicates if this value is less than the one passed

Signature

boolean lt( fNumber|string $number, integer $scale=NULL )

Parameters

fNumber|string $number The number to compare to
integer $scale The number of decimal places to compare - will use all available if not specified

Returns

If this number is less than the one passed

Throws

fValidationException
When $number is not a valid number

->lte() public

Indicates if this value is less than or equal to the one passed

Signature

boolean lte( fNumber|string $number, integer $scale=NULL )

Parameters

fNumber|string $number The number to compare to
integer $scale The number of decimal places to compare - will use all available if not specified

Returns

If this number is less than or equal to the one passed

Throws

fValidationException
When $number is not a valid number

->mod() public

Returns the remainder of dividing this number by the divisor provided. All floats are converted to integers.

Signature

fNumber mod( fNumber|string $divisor )

Parameters

fNumber|string $divisor The divisor - will be converted to an integer if it is a float

Returns

The remainder

Throws

fValidationException
When $divisor is not a valid number

->mul() public

Multiplies two numbers

Signature

fNumber mul( fNumber|string $multiplier, integer $scale=NULL )

Parameters

fNumber|string $multiplier The multiplier
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The product

Throws

fValidationException
When $multiplier is not a valid number

->neg() public

Negates this number

Signature

fNumber neg( integer $scale=NULL )

Parameters

integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The negated number

->pow() public

Raise this number to the power specified

Signature

fNumber pow( integer $exponent, integer $scale=NULL )

Parameters

integer $exponent The power to raise to - all non integer values will be truncated to integers
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The product

Throws

fValidationException
When $exponent is not a valid number

->powmod() public

Gets the remainder of this integer number raised to the integer $exponent, divided by the integer $modulus

This method is faster than doing $num->pow($exponent)->mod($modulus) and is primarily useful for cryptographic functionality.

Signature

fNumber powmod( fNumber|string $exponent, fNumber|string $modulus )

Parameters

fNumber|string $exponent The power to raise to - all non integer values will be truncated to integers
fNumber|string $modulus The value to divide by - all non integer values will be truncated to integers

Returns

The remainder

Throws

fValidationException
When $exponent or $modulus is not a valid number

->round() public

Rounds this number to the specified number of digits after the decimal - negative scales round the number by places to the left of the decimal

Signature

fNumber round( integer $scale )

Parameters

integer $scale The number of places after (or before if negative) the decimal to round to

Returns

The rounded result

->sign() public

Returns the sign of the number

Signature

integer sign( )

Returns

-1 if negative, 0 if zero, 1 if positive

->sqrt() public

Returns the square root of this number

Signature

fNumber sqrt( integer $scale=NULL )

Parameters

integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The square root

->sub() public

Subtracts two numbers

Signature

fNumber sub( fNumber|string $subtrahend, integer $scale=NULL )

Parameters

fNumber|string $subtrahend The subtrahend
integer $scale The number of places after the decimal - overrides the scale for this number

Returns

The difference

Throws

fValidationException
When $subtrahend is not a valid number

->trunc() public

Scales (truncates or expands) the number to the specified number of digits after the decimal - negative scales round the number by places to the left of the decimal

Signature

fNumber trunc( integer $scale )

Parameters

integer $scale The number of places after (or before if negative) the decimal

Returns

The square root