mdmoney 1.1.2 mdmoney: ^1.1.2 copied to clipboard
march.dev money library. Provides a new way to work with a money. From highly flexible creation to rich data manipulation and stringification options.
mdmoney #
march.dev money library. Provides a new way to work with a money. From highly flexible creation to rich data manipulation and stringification options.
Getting Started #
To begin your work with a money object you need to create it, there are several ways to do it:
Money.fromCents
with following args:cents
currency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromBigInt
with following args:BigInt
amountcurrency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromInt
with following args:int
amountcurrency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromDecimal
with following args:Decimal
amount (fromdecimal
package)currency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromDouble
with following args:double
amountcurrency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromString
with following args:String
currency
, if not specified in aString
- custom
precision
, if not provided -currency.precision
will be used instead
Money.fromAmount
with following args:Amount
amountcurrency
- custom
precision
, if not provided -amount.precision
will be used instead preferCurrencyPrecision
, if set totrue
fieldprecision
is omitted, otherwise eitherprecision
oramount.precision
will be used.
Also there are some convenient ways to create an object:
Money.zeroOf
to create the amount with0
as numerator with following args:currency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.oneOf
to create the amount with1
as numerator with following args:currency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.oneIntOf
to create the amount with1
as integer with following args:currency
- custom
precision
, if not provided -currency.precision
will be used instead
Money.zero
to create the amount with0
as numerator in default currency (FiatCurrency.$default
)Money.one
to create the amount with1
as numerator in default currency (FiatCurrency.$default
)Money.oneInt
to create the amount with1
as integer in default currency (FiatCurrency.$default
)
First of all, Money
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
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)toDecimal
- returns the amount inDecimal
toDouble
- returns the amount indouble
toAmount
- returns the amount inAmount
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
)
FiatCurrencyFormat
- specifies how currency should be displayed:none
code
(USD
/EUR
/UAH
/etc.)icon
($
/€
/₴
/etc.)
CurrencyPosition
- specifies where currency should be:start
startSpaced
end
endSpaced
decimalSeparator
Examples #
To see usage example navigate to the Example section.
Feature requests and Bug reports #
Feel free to post a feature requests or report a bug here.