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.
What is this? We will use %. 2f to limit a given floating-point number to two decimal places.
Now you can limit the decimal places. Select the number cell and in the Menu, go to Format > Number > More Formats > Custom number format.
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.
You use the String. format() method.
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|
+--------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With