在mongodb中舍入到小数点后2位

编程入门 行业动态 更新时间:2024-10-21 06:43:48
本文介绍了在mongodb中舍入到小数点后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的收藏为

学生

{ "first_name":"Harew", "last_name":"Jackson", "class":14, "fee": [ { "tuition":48500.2456, "transportation":500 } ] }

我需要根据fee = 4500.24过滤学生,并显示 所有有费用4500.24的学生都忽略了小数点后的其他数字.

I need to filter student according to fee = 4500.24 and it should display all the students having fee 4500.24 ignoring other digits after the decimal point.

我已经搜索了 MongoDB:如何在查询中获得N个小数精度 查询精度,但是此处提供的解决方案在我的情况下不起作用,因为 "$mod": [ "$amount.value", 0.01 ]不适用于BigDecimal类型,在我的收藏中,我的费用类型为BigDecimal.

I have searched in MongoDB: How to get N decimals precision in a query precision-in-a-query but the solution provided here does not work in my scenario since "$mod": [ "$amount.value", 0.01 ] is not applicable for BigDecimal type and in my collection I have fee type as BigDecimal.

以下解决方案似乎效果很好,但我不知道如何在Scala中实现该功能

The following solution seems to work well but I don't know how to implement this in Scala

db.collection.find({ "$where": function() { return Math.round(this.fee.school * 100)/ 100 === 1.12; } })

推荐答案

您可以轻松地将这些值从BigDecimal中舍入为特定精度,也可以同时将其转换为double值. 例如:-

you can easily round up the values into specific precision from BigDecimal , also if you want you can convert it into double value at the same time . For Example : -

scala> val s:BigDecimal = 10.232 s:BigDecimal = 10.232

scala> val s :BigDecimal = 10.232 s: BigDecimal = 10.232

scala> s.setScale(2,BigDecimal.RoundingMode.HALF_UP).toDouble res1:Double = 10.23//转换为Double

scala> s.setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble res1: Double = 10.23 // CONVERTED AS DOUBLE

scala> s.setScale(2,BigDecimal.RoundingMode.HALF_UP) res2:scala.math.BigDecimal = 10.23//正在路由

scala> s.setScale(2, BigDecimal.RoundingMode.HALF_UP) res2: scala.math.BigDecimal = 10.23 // Rouding Off

因此在scala中,而不是使用math.Round,您可以使用setScale.

So in scala instead of using math.Round you can use setScale.

更多推荐

在mongodb中舍入到小数点后2位

本文发布于:2023-07-06 07:59:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点   mongodb   中舍入到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!