npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ubc-farm/money

v3.1.0

Published

Utility class used to represent money

Downloads

6

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

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