3.1.0 • Published 8 years ago

@ubc-farm/money v3.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

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

new Money(thing)

ParamTypeDescription
thinganyvalue 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

ParamTypeDescription
digitsnumberif 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

ParamTypeDefaultDescription
localestring
optionsObject
options.parenthesesbooleanwrap negative numbers in parentheses
options.currencystring"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

ParamTypeDescription
centsnumberan 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

ParamTypeDefaultDescription
centsnumber
localestring
optionsObject
options.parenthesesbooleanwrap negative numbers in parentheses
options.currencystring"CAD"currency locale to return

stringToCents(str) ⇒ number

Converts a string representing money to a number representing cents

Kind: global function

ParamTypeDescription
strstringvalue converted into the Money value. Any non-digit values, except for the first . character, are stripped and the result is stored.
options.truncbooleantruncate fractional cents. If true, an integer will be returned.

floatToCents(float)

Converts a float representing dollars to a number representing cents

Kind: global function

ParamTypeDescription
floatnumberto convert into cents.
options.truncbooleantruncate fractional cents. If true, an integer will be returned.

Example

floatToCents(1.99) === 199

Example

floatToCents(8.959, { trunc: false }) === 895.9
3.1.1

8 years ago

3.1.0

8 years ago

3.0.0

9 years ago