Decimal
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
Precision
Signum
Parity (integer)
No description
No description
No description
No description
No description
No description
No description
No description
No description
String representation.
Integer representation.
Binary floating point representation.
Equality
Ordering
String representation.
JSON
Details
__construct(Decimal|string|int $value, int $precision = Decimal::DEFAULT_PRECISION)
Constructor
Initializes a new instance using a given value and minimum precision.
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.
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.
Decimal
copy(int|null $precision = null)
Copy
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.
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.
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.
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.
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.
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.
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.
Decimal
ln()
Natural logarithm
This method is equivalent in function to PHP's log
.
Decimal
exp()
Exponent
Decimal
log10()
Base-10 logarithm
Decimal
sqrt()
Square root
Decimal
floor()
Floor
Decimal
ceil()
Ceiling
Decimal
truncate()
Truncate
Decimal
round(int $places = 0, int $mode = Decimal::DEFAULT_ROUNDING)
Round
Decimal
shift(int $places)
Decimal point shift.
Decimal
trim()
Trims trailing zeroes.
int
precision()
Precision
int
signum()
Signum
int
parity()
Parity (integer)
Decimal
abs()
Absolute
Decimal
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.
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.
int
toInt()
Integer representation.
This method is equivalent to a cast to int.
float
toFloat()
Binary floating point representation.
This method is equivalent to a cast to float, and is not affected by the 'precision' INI setting.
bool
equals(mixed $other)
Equality
This method is equivalent to the ==
operator.
int
compareTo(mixed $other)
Ordering
This method is equivalent to the <=>
operator.
string
__toString()
String representation.
This method is equivalent to a cast to string, as well as toString
.
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
.