title: how should i store a price in mongoose?
body: <p>I'm using mongoose schemas for node.js along with express-validator (which has node-validator santiziations and validators).</p>
  
  <p>What's a good way to store price for an item?</p>
  
  <p>I currently have </p>
  
  <pre><code>var ItemSchema = new Schema({
      name            : { type: String, required: true, trim: true }
      , price             : Number
  });
  </code></pre>
  
  <p>Price is optional, so I have:</p>
  
  <pre><code>  if ( req.body.price ) {
      req.sanitize('price').toFloat();
      req.assert('price', 'Enter a price (number only)').isFloat();
    }
  </code></pre>
  
  <p>express-validator gives me isNumeric (allows 0 padding), isDecimal, and isInt...I'd rather just convert to decimal and strip all characters, so I'm always inserting 42.00 into db.</p>
  
  <p>I want to allow them to enter $42.00, $42, 42, 42.00   and just store 42.00. How can I accomplish this? and still validate that I'm seeing something resembling a number, for example if they enter 'abc' I want to throw an error back to the form using req.assert.</p>
  
  <p>Also, I suppose currency will eventually become an issue...</p>
  
  <p>Update, I found this post which says to store price as integer in cents, so 4200
  <a href="https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use">https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use</a></p>
  
  <p>I just need a way to convert 4200 to $42.00 when I call item.price and also sanitize and convert the input into 4200.</p>