march.dev money library. Provides a new way to work with a money. 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:
- Money.fromCentswith following args:- cents
- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromBigIntwith following args:- BigIntamount
- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromIntwith following args:- intamount
- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromDecimalwith following args:- Decimalamount (from- decimalpackage)
- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromDoublewith following args:- doubleamount
- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromStringwith following args:- String
- currency, if not specified in a- String
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.fromAmountwith following args:- Amountamount
- currency
- custom precision, if not provided -amount.precisionwill be used instead
- preferCurrencyPrecision, if set to- truefield- precisionis omitted, otherwise either- precisionor- amount.precisionwill be used.
 
Also there are some convenient ways to create an object:
- Money.zeroOfto create the amount with- 0as numerator with following args:- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.oneOfto create the amount with- 1as numerator with following args:- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.oneIntOfto create the amount with- 1as integer with following args:- currency
- custom precision, if not provided -currency.precisionwill be used instead
 
- Money.zeroto create the amount with- 0as numerator in default currency (- FiatCurrency.$default)
- Money.oneto create the amount with- 1as numerator in default currency (- FiatCurrency.$default)
- Money.oneIntto create the amount with- 1as 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 the- BigInt- centsrepresentation of the amount
- currency- returns the- currencyof the amount
- precision- returns the- precisionof the amount (quantity of digits in fractional part)
- sign- returns the- signof the amount
- isEven- whether the amount is- evenor not
- isOdd- whether the amount is- oddor not
- isNegative- whether the amount is- negativeor not
- isPositive- whether the amount is- positiveor not
- isZero- whether the amount is equals to- zeroor not
- isGreaterThanZero- whether the amount is greater than- zeroor not
- isGreaterThanOrEqualZero- whether the amount is greater than or equals to- zeroor not
- isLessThanZero- whether the amount is less than- zeroor not
- isLessThanOrEqualZero- whether the amount is less than or equals to- zeroor not
- integer- returns the- integerpart of the amount
- fractional- returns the- fractionalpart of the amount in- BigIntcents
- fractionalDecimal- returns the- fractionalpart of the amount in- Decimal
- fractionalDouble- returns the- fractionalpart of the amount in- double
- abs- returns the- absolute(always positive) amount
- round- returns the- roundedamount
- ceil- returns the- ceiledamount (rounded to the next integer)
- floor- returns the- flooredamount (truncating fractional part of the amount)
- toDecimal- returns the amount in- Decimal
- toDouble- returns the amount in- double
- toAmount- returns the amount in- Amount
- toString- return the- Stringrepresentation 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
 
- precision- if set, this method will behave differently based on- AmountFormat:- integer-- precisionis omitted
- fixedDouble-- precisionwill be used as an override to- Amount.precision
- flexibleDouble-- precisionwill be used only if length of fractionals will be less than- precision
 
 
- Max precision is 15, everything that is beyond this precision will be trimmed due to Decimal's internal inability to work with such precisions.
To see usage example navigate to the Example section.
Feel free to post a feature requests or report a bug here.