final class Decimal implements JsonSerializable (View source)

Constants

ROUND_UP

These constants are for auto-complete only.

ROUND_DOWN

ROUND_CEILING

ROUND_FLOOR

ROUND_HALF_UP

ROUND_HALF_DOWN

ROUND_HALF_EVEN

ROUND_HALF_ODD

ROUND_TRUNCATE

DEFAULT_ROUNDING

DEFAULT_PRECISION

MIN_PRECISION

MAX_PRECISION

Methods

__construct(Decimal|string|int $value, int $precision = Decimal::DEFAULT_PRECISION)

Constructor

static Decimal
sum(array|Traversable $values, int $precision = Decimal::DEFAULT_PRECISION)

Sum

static Decimal
avg(array|Traversable $values, int $precision = Decimal::DEFAULT_PRECISION)

Average

copy(int|null $precision = null)

Copy

add(Decimal|string|int $value)

Add

sub(Decimal|string|int $value)

Subtract

mul(Decimal|string|int $value)

Multiply

div(Decimal|string|int $value)

Divide

mod(Decimal|string|int $value)

Modulo (integer)

rem(Decimal|string|int $value)

Remainder

pow(Decimal|string|int $exponent)

Power

ln()

Natural logarithm

exp()

Exponent

log10()

Base-10 logarithm

sqrt()

Square root

floor()

Floor

ceil()

Ceiling

truncate()

Truncate

round(int $places = 0, int $mode = Decimal::DEFAULT_ROUNDING)

Round

shift(int $places)

Decimal point shift.

trim()

Trims trailing zeroes.

int
precision()

Precision

int
signum()

Signum

int
parity()

Parity (integer)

abs()

Absolute

negate()

Negate

bool
isEven()

No description

bool
isOdd()

No description

bool
isPositive()

No description

bool
isNegative()

No description

bool
isNaN()

No description

bool
isInf()

No description

bool
isInteger()

No description

bool
isZero()

No description

string
toFixed(int $places = 0, bool $commas = false, int $rounding = Decimal::DEFAULT_ROUNDING)

No description

string
toString()

String representation.

int
toInt()

Integer representation.

float
toFloat()

Binary floating point representation.

bool
equals(mixed $other)

Equality

int
compareTo(mixed $other)

Ordering

string
__toString()

String representation.

mixed
jsonSerialize()

JSON

Details

__construct(Decimal|string|int $value, int $precision = Decimal::DEFAULT_PRECISION)

Constructor

Initializes a new instance using a given value and minimum precision.

Parameters

Decimal|string|int $value
int $precision

Exceptions

BadMethodCallException
TypeError
DomainException

static Decimal sum(array|Traversable $values, int $precision = Decimal::DEFAULT_PRECISION)

Sum

The precision of the result will be the max of all precisions that were encountered during the calculation. The given precision should therefore be considered the minimum precision of the result.

This method is equivalent to adding each value individually.

Parameters

array|Traversable $values
int $precision

Minimum precision of the sum.

Return Value

Decimal

the sum of all given values.

Exceptions

TypeError
ArithmeticError

static Decimal avg(array|Traversable $values, int $precision = Decimal::DEFAULT_PRECISION)

Average

The precision of the result will be the max of all precisions that were encountered during the calculation. The given precision should therefore be considered the minimum precision of the result.

This method is equivalent to adding each value individually, then dividing by the number of values.

Parameters

array|Traversable $values
int $precision

Minimum precision of the average.

Return Value

Decimal

the average of all given values.

Exceptions

TypeError
ArithmeticError

Decimal copy(int|null $precision = null)

Copy

Parameters

int|null $precision

The precision of the return value, which defaults to the precision of this decimal.

Return Value

Decimal

a copy of this decimal.

Decimal add(Decimal|string|int $value)

Add

This method is equivalent to the + operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the result of adding this decimal to the given value.

Exceptions

TypeError

Decimal sub(Decimal|string|int $value)

Subtract

This method is equivalent to the - operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the result of subtracting a given value from this decimal.

Exceptions

TypeError

Decimal mul(Decimal|string|int $value)

Multiply

This method is equivalent to the * operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the result of multiplying this decimal by the given value.

Exceptions

TypeError

Decimal div(Decimal|string|int $value)

Divide

This method is equivalent to the / operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the result of dividing this decimal by the given value.

Exceptions

TypeError
DivisionByZeroError
ArithmeticError

Decimal mod(Decimal|string|int $value)

Modulo (integer)

This method is equivalent to the % operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the remainder after dividing the integer value of this decimal by the integer value of the given value

Exceptions

TypeError
DivisionByZeroError
ArithmeticError

See also

Decimal::rem for the decimal remainder.

Decimal rem(Decimal|string|int $value)

Remainder

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $value

Return Value

Decimal

the remainder after dividing this decimal by a given value.

Exceptions

TypeError
DivisionByZeroError
ArithmeticError

Decimal pow(Decimal|string|int $exponent)

Power

This method is equivalent to the ** operator.

The precision of the result will be the max of this decimal's precision and the given value's precision, where scalar values assume the default.

Parameters

Decimal|string|int $exponent

The power to raise this decimal to.

Return Value

Decimal

the result of raising this decimal to a given power.

Exceptions

TypeError

Decimal ln()

Natural logarithm

This method is equivalent in function to PHP's log.

Return Value

Decimal

the natural logarithm of this decimal (log base e), with the same precision as this decimal.

Decimal exp()

Exponent

Return Value

Decimal

the exponent of this decimal, ie. e to the power of this, with the same precision as this decimal.

Decimal log10()

Base-10 logarithm

Return Value

Decimal

the base-10 logarithm of this decimal, with the same precision as this decimal.

Decimal sqrt()

Square root

Return Value

Decimal

the square root of this decimal, with the same precision as this decimal.

Decimal floor()

Floor

Return Value

Decimal

the closest integer towards negative infinity.

Decimal ceil()

Ceiling

Return Value

Decimal

the closest integer towards positive infinity.

Decimal truncate()

Truncate

Return Value

Decimal

the integer value of this decimal.

Decimal round(int $places = 0, int $mode = Decimal::DEFAULT_ROUNDING)

Round

Parameters

int $places

The number of places behind the decimal to round to.

int $mode

The rounding mode, which are constants of Decimal.

Return Value

Decimal

the value of this decimal with the same precision, rounded according to the specified number of decimal places and rounding mode

Exceptions

InvalidArgumentException

Decimal shift(int $places)

Decimal point shift.

Parameters

int $places

The number of places to shift the decimal point by. A positive shift moves the decimal point to the right, a negative shift moves the decimal point to the left.

Return Value

Decimal

A copy of this decimal with its decimal place shifted.

Decimal trim()

Trims trailing zeroes.

Return Value

Decimal

A copy of this decimal without trailing zeroes.

int precision()

Precision

Return Value

int

the precision of this decimal.

int signum()

Signum

Return Value

int

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

int parity()

Parity (integer)

Return Value

int

0 if the integer value of this decimal is even, 1 if odd. Special numbers like NAN and INF will return 1.

Decimal abs()

Absolute

Return Value

Decimal

the absolute (positive) value of this decimal.

Decimal negate()

Negate

Return Value

Decimal

the same value as this decimal, but the sign inverted.

bool isEven()

No description

Return Value

bool

TRUE if this decimal is an integer and even, FALSE otherwise.

bool isOdd()

No description

Return Value

bool

TRUE if this decimal is an integer and odd, FALSE otherwise.

bool isPositive()

No description

Return Value

bool

TRUE if this decimal is positive, FALSE otherwise.

bool isNegative()

No description

Return Value

bool

TRUE if this decimal is negative, FALSE otherwise.

bool isNaN()

No description

Return Value

bool

TRUE if this decimal is not a defined number.

bool isInf()

No description

Return Value

bool

TRUE if this decimal represents infinity, FALSE otherwise.

bool isInteger()

No description

Return Value

bool

TRUE if this decimal is an integer, ie. does not have significant figures behind the decimal point, otherwise FALSE.

bool isZero()

No description

Return Value

bool

TRUE if this decimal is either positive or negative zero.

string toFixed(int $places = 0, bool $commas = false, int $rounding = Decimal::DEFAULT_ROUNDING)

No description

Parameters

int $places

The number of places behind the decimal point.

bool $commas

TRUE if thousands should be separated by a comma.

int $rounding

Return Value

string

the value of this decimal formatted to a fixed number of decimal places, optionally with thousands comma-separated, using a given rounding mode.

string toString()

String representation.

This method is equivalent to a cast to string.

This method should not be used as a canonical representation of this decimal, because values can be represented in more than one way. However, this method does guarantee that a decimal instantiated by its output with the same precision will be exactly equal to this decimal.

Return Value

string

the value of this decimal represented exactly, in either fixed or scientific form, depending on the value.

int toInt()

Integer representation.

This method is equivalent to a cast to int.

Return Value

int

the integer value of this decimal.

Exceptions

OverflowException

float toFloat()

Binary floating point representation.

This method is equivalent to a cast to float, and is not affected by the 'precision' INI setting.

Return Value

float

the native PHP floating point value of this decimal.

Exceptions

OverflowException
UnderflowException

bool equals(mixed $other)

Equality

This method is equivalent to the == operator.

Parameters

mixed $other

Return Value

bool

TRUE if this decimal is considered equal to the given value. Equal decimal values tie-break on precision.

int compareTo(mixed $other)

Ordering

This method is equivalent to the <=> operator.

Parameters

mixed $other

Return Value

int

0 if this decimal is considered is equal to $other, -1 if this decimal should be placed before $other, 1 if this decimal should be placed after $other.

string __toString()

String representation.

This method is equivalent to a cast to string, as well as toString.

Return Value

string

the value of this decimal represented exactly, in either fixed or scientific form, depending on the value.

mixed jsonSerialize()

JSON

This method is only here to honour the interface, and is equivalent to toString. JSON does not have a decimal type so all decimals are encoded as strings in the same format as toString.

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.