march.dev amount library. Provides a new way to work with an amount. From highly flexible creation to rich data manipulation and stringification options.
To begin your work with a money object you need to create it, there are several ways to do it:
Amount
with following args:BigInt
numerator- custom
precision
, if not provided or if negative precision was set -Amount.defaultPrecision
will be used instead
Amount.fromNumerator
with following args:int
numerator- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Amount.fromBigInt
with following args:BigInt
amount- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Amount.fromInt
with following args:int
amount- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Amount.fromDecimal
with following args:Decimal
amount (fromdecimal
package)- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Amount.fromDouble
with following args:double
amount- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Amount.fromString
with following args:String
- custom
precision
, if not provided -Amount.defaultPrecision
will be used instead
Also there are some convenient ways to create an object:
Amount.zeroOf
to create the amount with0
as numerator with customprecision
Amount.oneOf
to create the amount with1
as numerator with customprecision
Amount.oneIntOf
to create the amount with1
as integer with customprecision
Amount.zero
to create the amount with0
as numerator with default precision (Amount.defaultPrecision
)Amount.one
to create the amount with1
as numerator with default precision (Amount.defaultPrecision
)Amount.oneInt
to create the amount with1
as integer with default precision (Amount.defaultPrecision
)
First of all, Amount
object is comparable
and has all required operators:
- unary
operator -
- binary
operator -
operator +
operator *
operator /
operator <
operator <=
operator >
operator >=
operator ==
Regarding what you can do with this object, let's break down following methods/getters/fields:
cents
- returns theBigInt
cents
representation of the amountcurrency
- returns thecurrency
of the amountprecision
- returns theprecision
of the amount (quantity of digits in fractional part)sign
- returns thesign
of the amountisEven
- whether the amount iseven
or notisOdd
- whether the amount isodd
or notisNegative
- whether the amount isnegative
or notisPositive
- whether the amount ispositive
or notisZero
- whether the amount is equals tozero
or notisGreaterThanZero
- whether the amount is greater thanzero
or notisGreaterThanOrEqualZero
- whether the amount is greater than or equals tozero
or notisLessThanZero
- whether the amount is less thanzero
or notisLessThanOrEqualZero
- whether the amount is less than or equals tozero
or notinteger
- returns theinteger
part of the amountfractional
- returns thefractional
part of the amount inBigInt
centsfractionalDecimal
- returns thefractional
part of the amount inDecimal
fractionalDouble
- returns thefractional
part of the amount indouble
toDecimal
- returns the amount inDecimal
toDouble
- returns theamount indouble
abs
- returns theabsolute
(always positive) amountround
- returns therounded
amountceil
- returns theceiled
amount (rounded to the next integer)floor
- returns thefloored
amount (truncating fractional part of the amount)toString
- return theString
representation of the amount with lots of customisation options, they are:DecimalSeparatorFormat
- specifies which decimal separator to use:point
comma
RankFormat
- specifies rank formatting:none
(XXXX
)space
(X XXX
)
AmountFormat
- specifies amount display formatting:integer
- only integer part (XXXX
)flexibleDouble
- fractional parts will not display trailing zeros (XXXX
/XXXX.X
/XXXX.XX
)fixedDouble
- fractional parts will display full precision, even zeros (XXXX.XX
)
To access default precision use following static getter:
Amount.defaultPrecision
And for changing default precision use following static method:
Amount.setDefaultPrecision(<new_precision>)
But be cautious, precision cannot be less than zero.
To see usage example navigate to the Example section.
Feel free to post a feature requests or report a bug here.