Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit decimal values to 2 digits before applying agg function?

I am following this solution 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?

For examples: I have values like below and the sum function sums it,

2.346
1.549

However I want the values to be rounded to 2 digit after the decimal like

2.35
1.55

before summing it. How can I do it? I was not able to find any sub function like sum().round of function sum.

Note: I am using Spark 1.5.1 version.

like image 839
Explorer Avatar asked Jan 17 '17 19:01

Explorer


People also ask

How do you limit a variable to two decimal places?

What is this? We will use %. 2f to limit a given floating-point number to two decimal places.

How do I restrict decimal points?

Now you can limit the decimal places. Select the number cell and in the Menu, go to Format > Number > More Formats > Custom number format.

How do I restrict decimals in Excel?

Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.

How do I restrict decimal places in Java?

You use the String. format() method.


1 Answers

You can use 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|
+--------+
like image 121
Psidom Avatar answered Oct 26 '22 21:10

Psidom