It’s also worth pointing out that there is a limit on the value of the biggest integer JavaScript can accurately represent. That’s available in Number.MAX_SAFE_INTEGER and is 253-1 (9007199254740991).
Thanks for all reply.
So the solution of this, we should use values in cents (as integer) by @serkandurusoy.
Eg: We will multiple all of value input with *100 (decimal number = 2 digits)
HI all, now I have problem when fetching data with Aggregate .
1- Meteor collection
let data = Collection.find, findOne
---
doc.decimalField.toNumber()
// Work fine with Decimal JS Method (Client/Server)
2- Use sakulstra:aggregate or Native node driver
let data = Collection.aggregate([.......]), Collection.rawCollection().aggregate([.....])
---
doc.decimalField.toNumber()
// Don't work with Decimal JS Method (Client/Server)
Collection.rawCollection().aggregate() returns a Promise.
If you are using it inside a Meteor Method, define your method as async and then await the result of the aggregation. You will also need to convert the aggregation cursor to an array. So something like this:
Yes I understand about this, but the problem is :
I can not convert Decimal field to Number by .toNumber() Decimal JS method on client/server side like .finde, findOne (Meteor collection)
The Meteor package mongo-decimal just adds a few extra methods and properties to decimal.js. It also adds EJSON compatibility.
You should probably start by looking at the NPM package linked above. I suspect you’ll probably need to convert your MongoDB value to a Decimal before you’re able to manipulate it with toNumber().
const decNum = new Decimal(mongoDecimalValue);
console.log(decNum.toNumber());
import { Decimal as DecJs } from 'decimal.js'
export const aggregateNumber = new ValidatedMethod({
name: 'demo.aggregateNumber'
validate: null,
run() {
if (Meteor.isServer) {
let data = NumberVal.aggregate([
{
$limit: 1,
},
])[0]
data.valDecimal = new DecJs(data.valDecimal)
return data
}
},
})
Error
Exception while invoking method 'demo.aggregateNumber' Error: [DecimalError] Invalid argument: 0.1
I20190523-19:42:22.037(7)? at new Decimal (/Volumes/DATA/MeteorApp/meteor-element/node_modules/decimal.js/decimal.js:4302:15)
I20190523-19:42:22.037(7)? at MethodInvocation.run (imports/api/demos/mongoDecimal.js:50:25)
I20190523-19:42:22.037(7)? at ValidatedMethod._execute (packages/mdg:validated-method/validated-method.js:94:12)
I20190523-19:42:22.037(7)? at MethodInvocation.demo.aggregateNumber (packages/mdg:validated-method/validated-method.js:57:23)
I20190523-19:42:22.038(7)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190523-19:42:22.038(7)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190523-19:42:22.038(7)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20190523-19:42:22.038(7)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190523-19:42:22.038(7)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1304:12)
I20190523-19:42:22.038(7)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190523-19:42:22.038(7)? at new Promise (<anonymous>)
I20190523-19:42:22.038(7)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190523-19:42:22.039(7)? at packages/ddp-server/livedata_server.js:559:clock430: