@ubc-farm/money
v3.1.0
Published
Utility class used to represent money
Downloads
2
Readme
Classes
Functions
Money
A class used to represent money. Internally the money value is stored as cents, so most of the time it will be an integer which can be used for math without floating point troubles. It can also be formatted with the toString() function, utilizing Number.toLocaleString().
Kind: global class
- Money
- new Money(thing)
- instance
- .dollars
- .cents
- .getDollars() ⇒ number
- .getCents(digits) ⇒ number
- .toInteger() ⇒ number
- .valueOf() ⇒ number
- .toString([locale], [options])
- .toJSON() ⇒ string
- static
new Money(thing)
| Param | Type | Description | | --- | --- | --- | | thing | any | value converted into the Money value. If a Money instead, returns a new Money with the same value. If a number, assumes that the number is a decimal representing dollars and cents and converts it to a string. If a string, any non-digit values, except for the first . character, are stripped and the result is stored. |
money.dollars
Same as getDollars()
Kind: instance property of Money
money.cents
Same as getCents(0)
Kind: instance property of Money
money.getDollars() ⇒ number
Kind: instance method of Money
Returns: number - the dollar value of this money
money.getCents(digits) ⇒ number
Returns the cents of this money.
Kind: instance method of Money
Returns: number - will always be between -100 and 100
| Param | Type | Description | | --- | --- | --- | | digits | number | if specified, toFixed will be called on the value before determining the cents value. |
money.toInteger() ⇒ number
Kind: instance method of Money
Returns: number - the integer value of this money, stripping
any fractional cents. Useful for doing money related math,
as integers won't suffer from floating point problems.
Example
new Money('$10.99').toInteger() === 1099
money.valueOf() ⇒ number
Kind: instance method of Money
Returns: number - float representation of the money
Will be NaN if the internal value is null.
money.toString([locale], [options])
Returns a formatted currency string. If value is NaN, an empty string is returned.
Kind: instance method of Money
See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
| Param | Type | Default | Description | | --- | --- | --- | --- | | [locale] | string | | | | [options] | Object | | | | [options.parentheses] | boolean | | wrap negative numbers in parentheses | | [options.currency] | string | "USD" | currency locale to return |
money.toJSON() ⇒ string
Kind: instance method of Money
Returns: string - The money as a simple string
Money.fromInteger(cents) ⇒ Money
Kind: static method of Money
Returns: Money - with a value based off the provided cent amount
| Param | Type | Description | | --- | --- | --- | | cents | number | an integer like that returned by Money#toInteger |
Example
Money.fromInteger(1099) == new Money('$10.99')
centsToString(cents, [locale], [options])
Returns a formatted currency string. If value is NaN, an empty string is returned.
Kind: global function
See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
| Param | Type | Default | Description | | --- | --- | --- | --- | | cents | number | | | | [locale] | string | | | | [options] | Object | | | | [options.parentheses] | boolean | | wrap negative numbers in parentheses | | [options.currency] | string | "CAD" | currency locale to return |
stringToCents(str) ⇒ number
Converts a string representing money to a number representing cents
Kind: global function
| Param | Type | Description | | --- | --- | --- | | str | string | value converted into the Money value. Any non-digit values, except for the first . character, are stripped and the result is stored. | | [options.trunc] | boolean | truncate fractional cents. If true, an integer will be returned. |
floatToCents(float)
Converts a float representing dollars to a number representing cents
Kind: global function
| Param | Type | Description | | --- | --- | --- | | float | number | to convert into cents. | | [options.trunc] | boolean | truncate fractional cents. If true, an integer will be returned. |
Example
floatToCents(1.99) === 199
Example
floatToCents(8.959, { trunc: false }) === 895.9