Number::of(any)
accepts any numeric value like floats, integers and even strings or other Number instances
e.g: Number::of("1.5")
, Number::of(1/3)
, Number::of(1)
, Number::of(Number::of(12.8))
are valid and correctly handled
On the other hand, the strings must be sane
e.g Number::of('1/3')
, Number::of('1,2')
, Number::of('1.5€')
will throw an exception
Basic instances are already built-in:
Number::zero()
alias for Number::of(0)Number::one()
alias for Number::of(1)Number::ten()
alias for Number::of(10)Number::hundred()
alias for Number::of(100)Number::thousand()
alias for Number::of(1000)
Number::sum(...any)
return a Number which values to the sum of the values in the collectionNumber::max(...any)
return a Number which values to the max value in the collectionNumber::min(...any)
return a Number which values to the min value in the collectionNumber::average(...any)
return a Number which values to the average value of the collection
Those methods accepts a list of valid values (e.g: Number::sum("2", 4, 1/2)
) as well as an iterable object (e.g: Number::sum(["2", 4, 1/2])
)
plus(any)
returns a Number which values to the sum between the two Numbersminus(any)
returns a Number which values to the difference between the two Numbersmultiply(any)
returns a Number which values to the multiplication between the two Numbersdivide(any)
returns a Number which values to the division between the two Numberspower(exponent)
returns a Number which values powered to the exponentsquare()
alias for power(2)cube()
alias for power(3)absolute()
returns a Number which value is positivenegate()
returns a Number which value will be positive if was negative, negative if positiveinverse()
returns a Number which values to one over the numberround(precision, mode)
returns a Number which values will rounded according to parameters, defaults to 15 digits and upround(precision)
returns a Number which decimals will be truncated according to precision without any rounding
compare(any)
returns -1 if Number is less, 1 if Number is greater and 0 if Numbers are equal, like strcmp does for stringsequals(any)
returns whether the two Numbers are equal. It handles floats epsilon comparison.isDifferent(any)
returns the inverse of equalsisGreater(any)
returns the strict superiority comparison resultisGreaterOrEqual(any)
returns the superiority comparison resultisLess(any)
returns the strict inferiority comparison resultisLessOrEqual(any)
returns the inferiority comparison result
wholePart()
returns a Number which value is the left part of the floating point (e.g 3.52 is 3.0)decimalPart()
returns a Number which value is the right part of the floating point (e.g 3.52 is 0.52)isPositive()
returns whether the Number value is positive or notisNegative()
returns whether the Number value is negative or notisZero()
returns whether the Number value is equal to zeroisWhole()
returns whether the Number has a decimal partapply(callback)
returns the callback result, useful for custom functionswhen(bool, callback)
returns the callback result if the condition is truthyformat(locale, decimals, forceDecimals)
returns the display format of the number in the desired locale (not for database storage!)
get()
returns the raw internal float value (for debug purposes)value()
returns the float value, preferred over value(), because the format will be right (no strange values like -0.0)sign()
returns+
or-
. N.B: Zero will be positive.
All accessors can be retrieved as properties like ->value
or ->sign
.
While floats you be fine for most usages, please read What Every Programmer Should Know About Floating-Point Arithmetic, so you will be aware of its limits. Calc tries to overcome it as much as possible (see the equals()
method implementation), but still some edge cases can occur.
If so, new tests are welcomed (please PR).