Mongodb convert amount into 0E-14

717 views
Skip to first unread message

Pawan Saini

unread,
May 3, 2019, 12:31:31 AM5/3/19
to mongodb-user
Hi Everyone, 

I am working on some kind of trading platform. So i have maintain balance of account of users. When trading happen, order are settle, at that time transactions are created very fast on same account.

So I am using $inc function to add or subtract amount from user accounts. I faced lots of issue because when i was  subtracting fractional values e.g.  0.001 from 0.003 then $inc function create issue. i was not getting correct value.

Now my balance field data type is Decimal128. my previous issue was resolved but it convert value like this .e.g 1.45057300000000.   if i keep subtracting amount from this balance. Mongodb convert it into 
0E-14.

Anyone can help me.  I want my amount maximum with 6 decimal and correct amount. if i have 0.00000001 this much balance in account then database will show me same value.

Is it possible??????

If someone help me. i will appreciate.



Thanks 

 
111111.PNG

Stephen Steneker

unread,
May 7, 2019, 6:39:28 AM5/7/19
to mongodb-user
On Friday, 3 May 2019 14:31:31 UTC+10, Pawan Saini wrote:
I am working on some kind of trading platform. So i have maintain balance of account of users. When trading happen, order are settle, at that time transactions are created very fast on same account.

So I am using $inc function to add or subtract amount from user accounts. I faced lots of issue because when i was  subtracting fractional values e.g.  0.001 from 0.003 then $inc function create issue. i was not getting correct value.

Now my balance field data type is Decimal128. my previous issue was resolved but it convert value like this .e.g 1.45057300000000.   if i keep subtracting amount from this balance. Mongodb convert it into 
0E-14.

Hi Pawan,

If you want to have precision for fractional values you should be using the Decimal128 type for all of the values in your calculation (amounts as well as balances).

From your description it sounds like you identified this issue and fixed your balance field, but have some lingering data that is still floating point.

Can you provide some more detail on how you are calculating the balance? Example documents with a code snippet (and your specific driver & version) would be helpful.

Note: you may find the $type query operator useful to check that values are of the expected BSON type. For example, in the mongo shell you could search for all values of a field that are not the expected type of "decimal":

   db.mycollection.find({myamount:{ $not: { $type: "decimal"}}})

Regards,
Stennie  

Pawan Saini

unread,
May 7, 2019, 7:20:07 AM5/7/19
to mongodb-user
Mongo DB Version : 4.0.6
Mongodb Driver (Nodejs) : 3.1.4
Mongoose : 5.2.10

Model : 

var CwalletSchema = new mongoose.Schema({
    userid: {
        type: mongoose.Schema.ObjectId,
        required: true,
        trim: true,
    },
    walletid: {
        type: String,
        trim: true,
    },
    walletType: {
        type: String,
        trim: true,
        required: true
    },
    balance: {
        type: mongoose.Schema.Types.Decimal128,
        required: true,
        default:0.00,
    },
    availableBalance: {
        type: mongoose.Schema.Types.Decimal128,
        required: true,
        default:0.00,
    },
    currency: {
        type: String,
        required: true,
    },
    label: {
        type: String,
        trim: true
    },
    details:{
        type:Object,
        trim: true,
    },
    updatedAt: {
        type : Date,
        default : Date.now
    }
});

Code : 
var AvailableBalance = 3000;
OR
var AvailableBalance = -3000;

var upObj={
$inc: {
availableBalance: AvailableBalance
}
}
var upRows = await walletModel.update({_id:mongoose.Types.ObjectId(wallbalance._id)}, upObj);

Stephen Steneker

unread,
May 7, 2019, 9:32:07 AM5/7/19
to mongodb-user
On Tuesday, 7 May 2019 21:20:07 UTC+10, Pawan Saini wrote:
Mongo DB Version : 4.0.6
Mongodb Driver (Nodejs) : 3.1.4
Mongoose : 5.2.10

Hi Pawan,

Thanks for the additional details to help reproduce this issue. After further testing, I realised that numbers less than 10^-6  will end up displaying in scientific notation (0E...) rather than decimal notation. This is apparently an expected implementation detail per the Decimal128 spec for a "To String Representation".

The underlying decimal value is correct, but the scientific string output is not helpful for your use case. There doesn't seem to be an obvious way to override this display default, but I'll look into this a bit further with the Node driver.

I want my amount maximum with 6 decimal and correct amount

If you are able to round or truncate your values to 6 decimal points, that would coincidentally avoid this display issue. Are values like 0.00000001 the result of previous floating point error, or are they expected for your use case?

Regards,
Stennie 

Pawan Saini

unread,
May 7, 2019, 9:58:56 AM5/7/19
to mongodb-user
Hi Stephen Steneker,

Thanks so much for you efforts.
---
If you are able to round or truncate your values to 6 decimal points, that would coincidentally avoid this display issue. Are values like 0.00000001 the result of previous floating point error, or are they expected for your use case?
----
i can round  or truncate value that to be incremented or decremented from balance but i can not  round or truncate value after  addition or subtraction.   If I first get balance from database then do increment or decrements  and apply round or truncate to value and update in database. but this method was not worked in my case. I already tried it.  I have to perform multiple transaction one same balance in friction of seconds
Reply all
Reply to author
Forward
0 new messages