mdamount 1.0.1
mdamount: ^1.0.1 copied to clipboard
march.dev amount library. Provides a new way to work with an amount. From highly flexible creation to rich data manipulation and stringification options.
mdamount #
march.dev amount library. Provides a new way to work with an amount. From highly flexible creation to rich data manipulation and stringification options.
Getting Started #
Construction #
To begin your work with a money object you need to create it, there are several ways to do it:
Amount.fromNumeratorwith following args:valuewhich is a numerator.- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Amount.fromBigIntwith following args:BigIntamount- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Amount.fromIntwith following args:intamount- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Amount.fromDecimalwith following args:Decimalamount (fromdecimalpackage)- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Amount.fromDoublewith following args:doubleamount- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Amount.fromStringwith following args:Stirng- custom
precision, if not provided -Amount.defaultPrecisionwill be used instead
Also there are some convenient ways to create an object:
Amount.zeroOfwith customprecisionAmount.oneOfto create one amount with customprecisionAmount.zeroto create zero amount with default precision (Amount.defaultPrecision)Amount.oneto create one amount with default precision (Amount.defaultPrecision)
Manipulation #
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 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 indoubletoDecimal- returns the amount inDecimaltoDouble- returns theamount indoubleabs- returns theabsolute(always positive) amountround- returns theroundedamountceil- returns theceiledamount (rounded to the next integer)floor- returns theflooredamount (truncating fractional part of the amount)toString- 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)
MoneyFormat- 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)
Changing or accessing default precision #
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.
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.