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

math-money

v0.1.1

Published

Floating point free Money objects and operations. Safe for math.

Downloads

7

Readme

Math Money

Math money is a module aimed for safe currency manipulation, by not operating with pure floating points.

Installation

    npm install math-money

Quickstart

    var Money = require('math-money');

    var oneDollar = Money.Dollar(1); //create using an integer
    var oneEuro = Money.Euro('12.00'); //create using a string

    var result = Money.Dollar(12).plus( Money.Dollar('10.00') );
    console.log( result.format() ); // US$ 22.00

Defining Your Currency

    var Money = require('math-money');
    var MyCurrency = Money.factory('MyCurrency', {
        decimals: 2,
        prefix: 'My$'
    });

    console.log( MyCurrency(10).format() ); //My$ 10.00

To create a custom currency, use Money.factory method. It accepts the currency identifier as first parameter and a config object as second, with the following fields:

  • decimals (how many decimal places the currency supports)
  • prefix (the prefix to format the currency. Normally you should put the currency symbol)

Operations

Money objects are immutable, so all math operations will not modify the object itself, but return another one.

  • Plus

Money.Dollar(1).plus( Money.Dollar(2) ) Should return a Money.Dollar(3);

  • Minus

Money.Dollar(2).minus( Money.Dollar(1) ) Should return a Money.Dollar(1)

  • isMoreThan

Money.Dollar(2).isMoreThan( Money.Dollar(1) ) Should return a true if money value is bigger that the compared one

  • isLessThan

Money.Dollar(1).isLessThan( Money.Dollar(2) ) Should return a true if money value is lower that the compared one

  • equals

Money.Dollar(1).equals( Money.Dollar(1) ) Should return a true if money value and the compared one are equal

Different currencies cannot interoperate. If you try to add, subtract or compare different currencies, an exception will be thrown.

Convenience Methods

  • Currency.raw
    var Money = require('math-money');
    Money.Dollar.raw( 2000 ); // US$ 20.00
    Money.Yen.raw(2000); // ¥ 2000

This generates a Money object built with a raw integer value. This value will then be converted to decimal if needed.

  • Currency.ZERO
    var Money = require('math-money');
    Money.Dollar.ZERO; // US$ 0.00
    Money.BRL.ZERO; // R$ 0.00
    Money.Yen.ZERO; // ¥ 0

Returns a money value representing no money.

Built in currencies

math-currency currently supports these currencies out of the box:

  • Brazilian Real Money.BRL
  • Euro Money.Euro or Money.EUR
  • Japanese Yen Money.Yen or Money.JPY
  • US Dollar Money.Dollar or Money.USD)