scala 在应用agg函数之前如何将十进制值限制为2位数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/41705575/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to limit decimal values to 2 digits before applying agg function?
提问by Explorer
I am following thissolution from one of the stack overflow post, my only requirement here is how can I limit the values that I want to sum to 2 digit after the decimal before applying the df.agg(sum())function?
我正在从堆栈溢出帖子之一中遵循此解决方案,我唯一的要求是如何在应用该df.agg(sum())函数之前将要求和的值限制为小数点后的 2 位数字?
For examples: I have values like below and the sum function sums it,
例如:我有如下值,并且 sum 函数将它相加,
2.346
1.549
However I want the values to be rounded to 2 digit after the decimal like
但是我希望这些值在小数点后四舍五入到 2 位,例如
2.35
1.55
before summing it. How can I do it? I was not able to find any sub function like sum().roundof function sum.
在总结之前。我该怎么做?我无法找到任何类似sum().roundfunction 的子函数sum。
Note: I am using Spark 1.5.1 version.
注意:我使用的是 Spark 1.5.1 版本。
回答by Psidom
You can use bround:
您可以使用bround:
val df = Seq(2.346, 1.549).toDF("A")
df.select(bround(df("A"), 2)).show
+------------+
|bround(A, 2)|
+------------+
|        2.35|
|        1.55|
+------------+
df.agg(sum(bround(df("A"), 2)).as("appSum")).show
+------------------+
|            appSum|
+------------------+
|3.9000000000000004|
+------------------+
                                          ^
df.agg(sum(df("A")).as("exactSum")).show
+--------+
|exactSum|
+--------+
|   3.895|
+--------+
回答by Explorer
The above solution does work for spark 2.0 version however for folks like me who are still using 1.5.*+ versions below is something that will work.(I used round function as suggested by @Psidom):
上面的解决方案确实适用于 spark 2.0 版本,但是对于像我这样仍在使用 1.5.*+ 版本的人来说是可行的。(我使用了@Psidom 建议的圆形函数):
val df = Seq(2.346, 1.549).toDF("A")
df.select(bround(df("A"), 2)).show
+------------+
|bround(A, 2)|
+------------+
|        2.35|
|        1.55|
+------------+
val total=df.agg(sum(round(df.col(colName),2)).cast("double")).first.getDouble(0)
total: Double = 3.90

