使用 MongoDB 聚合框架四舍五入到小数点后 2 位 [英] Rounding to 2 decimal places using MongoDB aggregation framework
问题描述
我正在使用 mongodb 聚合框架并进行如下所示的一些计算
I am using the mongodb aggregation framework and doing some calculations as shown below
db.RptAgg.aggregate(
{$group: {
_id: {Region: "$RegionTxt", Mth: "$Month"},
ActSls: {$sum:"$ActSls"},
PlnSls: {$sum:"$PlnSls"}
}},
{$project: {
ActSls: 1,
PlnSls: 1,
ActToPln: {$cond: [
{$ne: ["$PlnSls", 0]},
{$multiply: [{$divide: ['$ActSls', '$PlnSls']}, 100]},
0
]}
}}
);
我正在尝试找出将我的结果四舍五入到小数点后 2 位的最佳和最简单的方法.以下是我的结果
I am trying to figure out what is the best and easiest way to round my results to 2 decimal places. Following is my result
{
"result": [{
"_id": {
"Region": "East",
"Mth": 201301
},
"ActSls": 72,
"PlnSls": 102,
"ActToPln": 70.58823529411765
}],
"ok": 1
}
我想要ActToPln"显示 70.59 而不是 ActToPln":70.58823529411765,在聚合框架本身的结果中.我想避免在我的应用程序中进行舍入
I want "ActToPln" to show 70.59 instead of "ActToPln" : 70.58823529411765, in the results from aggegation framework itself. I want to avoid doing the rounding in my application
你能帮忙吗?
以下是我使用的数据集.
Following is the dataset i used.
{
"_id" : ObjectId("51d67ef69557c507cb172572"),
"RegionTxt" : "East",
"Month" : 201301,
"Date" : "2013-01-01",
"ActSls" : 31,
"PlnSls" : 51
}
{
"_id" : ObjectId("51d67ef69557c507cb172573"),
"RegionTxt" : "East",
"Month" : 201301,
"Date" : "2013-01-02",
"ActSls" : 41,
"PlnSls" : 51
}
提前致谢.南都
推荐答案
没有 $round
操作符,但是你可以在聚合框架中这样做——按特定顺序执行通常可以避免浮点数精度问题.
There is no $round
operator but you can do this in the aggregation framework - doing it in specific order will usually avoid floating point precision issues.
> db.a.save({x:1.23456789})
> db.a.save({x:9.87654321})
> db.a.aggregate([{$project:{ _id:0,
y:{$divide:[
{$subtract:[
{$multiply:['$x',100]},
{$mod:[{$multiply:['$x',100]}, 1]}
]},
100]}
}}])
{ "y" : 1.23 }
{ "y" : 9.87 }
鉴于问题中存在的管道,替换:
Given the existing pipeline in the problem, replace:
{$multiply:[{$divide: ['$ActSls', '$PlnSls']},100]}
与
{$divide:[
{$subtract:[
{$multiply:[
{$divide: ['$ActSls','$PlnSls']},
10000
]},
{$mod:[
{$multiply:[{$divide: ['$ActSls','$PlnSls']}, 10000 ]},
1]}
]},
100
]}
使用您的示例数据点,结果如下:
With your sample data points this is the result:
{ "ActSls" : 31, "PlnSls" : 51, "ActToPln" : 60.78 }
{ "ActSls" : 41, "PlnSls" : 51, "ActToPln" : 80.39 }
{ "ActSls" : 72, "PlnSls" : 102, "ActToPln" : 70.58 }
这篇关于使用 MongoDB 聚合框架四舍五入到小数点后 2 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!