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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-currency

v0.3.0

Published

Adds schema type "currency" to mongoose. Great for converting user input.

Downloads

699

Readme

What it does

  • Saves a String as an integer (by stripping non digits and multiplying by 100) to prevent rounding errors when performing calculations (See gotchas for details)
  • Strips out symbols from the beginning of strings (sometimes users include the currency symbol)
  • Strips out commas (sometimes users add in commas or copy paste values into forms, e.g. "1,000.50)
  • Only save from two digits past the decimal point ("$500.559" is converted to 50055 and doesn't round)
  • Strips [a-z, A-Z] from strings
  • Pass in a string or a number. Numbers will be stored AS IS.
  • Assumes that if you set the value to an integer you have already done the conversion (e.g. 50000 = $500.00)
  • If a floating point number is passed in it will round it. (500.55 -> 501). Just pass in integers to be safe.

How to use

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Will add the Currency type to the Mongoose Schema types
require('mongoose-currency').loadType(mongoose);
var Currency = mongoose.Types.Currency;

// If you don't have the Currency variable declared you can use 'mongoose.Types.Currency'
var ProductSchema = Schema({
  price: { type: Currency }
});

var Product = mongoose.model('Product', ProductSchema);

var product = new Product({ price: "$1,200.55" });
product.price; // Number: 120055
product.price = 1200;
product.price; // Number 1200 It will not round or multiply. Stored AS IS and should represent $12.00

Schema options

Accepts mongoose number schema options

// Will validate that the minimum is $200.00 and max is $500.00
var ProductSchema = Schema({
  price: { type: Currency, required: true, min: -20000, max: 50000 }
});

// See Gotchas (bottom) for details

Displaying to end users (views, reports, etc.)

To display values to end users remember to call .toFixed(2)

product.price.toFixed(2); // Returns 1200.55

Testing

Make sure to have mocha installed npm install mocha

in the root directory of the project run mocha test

Todo

  • Add currency validation?
  • Add option for what Currency strips by default (, or .) Will work better in countries where values are represented like this: 1.000.000,00
  • Add option for precision
  • Add currency validator?
  • Change min/max validators to accept currency format e.g. 200 is $200.00 not $2.00. Not sure if this is the best way or not.

Gotchas

The currency is returned as an integer. Why? Because adding two floating points together can have rounding errors.

// Not good
1.03+1.19; // 2.2199999999999998

Calculations with currency with mongoose-currency.

var product1 = Product.findById('id');
var product2 = Product.findById('id2');
product1.price; // returns 103 which represents $1.03
product2.price; // returns 119 which represents $1.19
var sum = product1.price+product2.price / 100;
sum.toFixed(2); // returns a number: 2.22

Displaying values for end users

var record = Product.findById('yourid');
record.price; // 10050 which represents $100.50
record.price.toFixed(2); // return 100.50

When you set an integer it will NOT be multiplied by 100. It will be stored AS IS! This is on purpose. This library is mainly for accepting USER generated inputs from forms, etc.

product.price = 100;
product.price; // Returns 100 Does not multiply by 100. Represents $1.00

Queries and validators

Remember when doing queries that to find values greater than, less than, etc. you need to multiply by 100!

So to get values greater than $100.00 you need to run your query with 100 * 100 e.g. 10000

It is considered best practice to store money values as ints. It will cause far fewer problems down the road

For further reading: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Just remember to call toFixed(2) whenever you want to display those values to end users.

Bitdeli Badge