Multiply your numbers by the amount of precision you need and use integers (*1000 for 3 d.p)? Using floating point to store money amounts seems fraught with rounding errors.
B. On Tue, Sep 1, 2009 at 9:58 AM, Metin Akat<[email protected]> wrote: > /* Paste this in a code editor to have code highlighting */ > > /* Here is my Map function */ > function(doc) { > if (doc.type == "Transaction") { > for (var i = 0; i < doc.accounts.length; i++) { > emit(doc.accounts[i].account, > doc.accounts[i].value * > doc.accounts[i].operation > ); > } > } > } > /* Here the value is the money transferred to or from an account and > the operation an integer (1 or -1) showing the type (credit debit) > of the operation */ > > /* This is the reduce */ > function(keys, values) { > return sum(values); > } > > > /* When I visit: > http://localhost:5984/nola7accounting/_design/accounting/_view/balance?group=true > I get this. The 13.05(9) is the sum of 5.03 + 8.03 (so is -13.05(9), > only it's negative). */ > {"rows":[ > {"key":"5cf15107d977498ec4003dca0d44c579","value":-13.059999999999999}, > {"key":"b4b0c38efd4ff4c27fc63d24ae981f60","value":13.059999999999999} > ]} > > /* But when I visit: > http://localhost:5984/nola7accounting/_design/accounting/_view/balance > In the accounting world, this should return 0 as it is the sum of all > values (and the sum of all operations in a financial system must be > 0), > but here is what I get: */ > {"rows":[ > {"key":null,"value":-8.881784197001252e-16} > ]} > > /* OK, the above is is something close to 0, but the question is... > how good is this for a financial software system? > The situation is the same, no matter if I store floats or strings > (which would represent decimals). This is probably the result > of the weak typing in javascript (JSON). > I wrote this view following along the "Banking" chapter of the CouchDB > book, here: > http://books.couchdb.org/relax/receipts/banking > The example there is nice and working, but it works with whole numbers > (integers). > So, the first question is... Is there some way of working with fixed > precision numbers in CouchdB? > And the further question is... How good is CouchDB (or any JSON > storage) for an accounting application? > */ >
