How to truncate a number to 3 decimals
Asked Answered
L

3

8

I do not know how to round a number in MongoDB. I only find how to do it with 2 decimals but not with more decimals.

"location" : {
    "type" : "Point",
    "coordinates" : [ 
        -74.00568, 
        40.70511
    ]
}

These is an example of a coordinate that I need to round with 3 numbers after the dot. Thank you

Leasehold answered 31/10, 2017 at 14:58 Comment(7)
do you want to round or truncate?Cushing
I want to truncate @JasonVLeasehold
Possible duplicate of meteor, mongodb, spacebars, how do I display only 2 decimal placesRattle
no Fabrio, 2 decimals is a specific functionCushing
@AbdelDOOFENSCHMIRTZ are you trying to add to the DB or read from the DB? does TRUNCATE(N, D); not work? (n is the number and d is decimal places requested?)Cushing
@JasonV I am trying to read from te DB but truncate or round are not implemented methods from MongoDBLeasehold
Can you show us the code you use to get your data from mongoDB?Billfold
W
7

For 3 decimal rounding, you can use this formula.

$divide: [ {$trunc: { $multiply: [ "$$coordinate" , 1000 ] } }, 1000 ]

For example, with your sample data, and using this aggregation:

db.getCollection('Test2').aggregate([
    { $project : 
        { 
            "location.type" : "$location.type",
            "location.coordinates" :  
            { 
                $map: 
                {
                    input: "$location.coordinates",
                    as: "coordinate",
                    in: { $divide: [ {$trunc: { $multiply: [ "$$coordinate" , 1000 ] } }, 1000 ] }
              }
            }   
        } 
    }
])

you can obtain the desired result.

{
    "_id" : ObjectId("59f9a4c814167b414f6eb553"),
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            -74.005, 
            40.705
        ]
    }
}
Wildon answered 1/11, 2017 at 11:55 Comment(0)
S
1

Starting Mongo 4.2, there is a new $trunc aggregation operator which can be used to truncate a number to a specified decimal place:

{ $trunc : [ <number>, <place> ] }

Which can be used as such within an aggregation pipeline (here we truncate xs to 3 decimal places):

// db.collection.insert([{x: 1.23456}, {x: -9.87654}, {x: 0.055543}, {x: 12.9999}])
db.collection.aggregate([{ $project: { "trunc_x": { $trunc: ["$x", 3] }}}])
// [{"trunc_x": 1.234}, {"trunc_x": -9.876}, {"trunc_x": 0.055}, {"trunc_x": 12.999}]

Note that the place parameter is optional, and omitting it results in truncating to a whole integer (i.e. truncating at 0 decimal places).

Also note the equivalent $round operator if you're interested in rounding rather than truncating.

Stuccowork answered 14/3, 2019 at 19:31 Comment(0)
P
0

With coordinates (as you have it in an array) you can trunc them in 2 steps:

  1. $arrayElemAt
  2. $trunc

to put it in motion:

 {'$project':{ 
    'lat': {'$trunc': [{ '$arrayElemAt': [ "$location.coordinates", 0 ] },2]},
    'lon': {'$trunc': [{ '$arrayElemAt': [ "$location.coordinates", 1 ] },2]}, 
 },

I assume your first value in the coordinates is the latitude.

Paragraphia answered 19/8, 2020 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.