mdmoney 1.1.7
mdmoney: ^1.1.7 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.fromCentswith following args:centscurrency- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromBigIntwith following args:BigIntamountcurrency- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromIntwith following args:intamountcurrency- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromDecimalwith following args:Decimalamount (fromdecimalpackage)currency- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromDoublewith following args:doubleamountcurrency- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromStringwith following args:Stringcurrency, if not specified in aString- custom
precision, if not provided -currency.precisionwill be used instead
Money.fromAmountwith following args:Amountamountcurrency- custom
precision, if not provided -amount.precisionwill be used instead preferCurrencyPrecision, if set totruefieldprecisionis omitted, otherwise eitherprecisionoramount.precisionwill be used.
Also there are some convenient ways to create an object:
Money.zeroOfto create the amount with0as numerator with following args:currency- custom
precision, if not provided -currency.precisionwill be used instead
Money.oneOfto create the amount with1as numerator with following args:currency- custom
precision, if not provided -currency.precisionwill be used instead
Money.oneIntOfto create the amount with1as integer with following args:currency- custom
precision, if not provided -currency.precisionwill be used instead
Money.zeroto create the amount with0as numerator in default currency (FiatCurrency.$default)Money.oneto create the amount with1as numerator in default currency (FiatCurrency.$default)Money.oneIntto create the amount with1as 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 theBigIntcentsrepresentation of the amountcurrency- returns thecurrencyof the amountprecision- returns theprecisionof the amount (quantity of digits in fractional part)sign- returns thesignof the amountisEven- whether the amount isevenor notisOdd- whether the amount isoddor notisNegative- whether the amount isnegativeor notisPositive- whether the amount ispositiveor notisZero- whether the amount is equals tozeroor notisGreaterThanZero- whether the amount is greater thanzeroor notisGreaterThanOrEqualZero- whether the amount is greater than or equals tozeroor notisLessThanZero- whether the amount is less thanzeroor notisLessThanOrEqualZero- whether the amount is less than or equals tozeroor notinteger- returns theintegerpart of the amountfractional- returns thefractionalpart of the amount inBigIntcentsfractionalDecimal- returns thefractionalpart of the amount inDecimalfractionalDouble- returns thefractionalpart of the amount indoubleabs- returns theabsolute(always positive) amountround- returns theroundedamountceil- returns theceiledamount (rounded to the next integer)floor- returns theflooredamount (truncating fractional part of the amount)toDecimal- returns the amount inDecimaltoDouble- returns the amount indoubletoAmount- returns the amount inAmounttoString- return theStringrepresentation of the amount with lots of customisation options, they are:DecimalSeparatorFormat- specifies which decimal separator to use:pointcomma
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:nonecode(USD/EUR/UAH/etc.)icon($/€/₴/etc.)
CurrencyPosition- specifies where currency should be:startstartSpacedendendSpaceddecimalSeparator
precision- if set, this method will behave differently based onAmountFormat:integer-precisionis omittedfixedDouble-precisionwill be used as an override toAmount.precisionflexibleDouble-precisionwill be used only if length of fractionals will be less thanprecision
Known limitations #
- Max precision is
15, everything that is beyond this precision will be trimmed due to Decimal's internal inability to work with such precisions.
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.