我很困惑在 spark sql 中 round 和 bround 是如何工作的。
scala> spark.sql("select round(1.5, 0), bround(1.5, 0)").show()
+-------------+--------------+
|round(1.5, 0)|bround(1.5, 0)|
+-------------+--------------+
| 2| 2|
+-------------+--------------+
scala> spark.sql("select round(2.5, 0), bround(2.5, 0)").show()
+-------------+--------------+
|round(2.5, 0)|bround(2.5, 0)|
+-------------+--------------+
| 3| 2|
+-------------+--------------+
scala> spark.sql("select round(3.5, 0), bround(3.5, 0)").show()
+-------------+--------------+
|round(3.5, 0)|bround(3.5, 0)|
+-------------+--------------+
| 4| 4|
+-------------+--------------+
最佳答案
- 圆形
Rounding mode to round towards {@literal "nearest neighbor"} unless both neighbors are equidistant, in which case round up. Behaves as for {@code
RoundingMode.UP
} if the discarded fraction is > 0.5; otherwise, behaves as for {@codeRoundingMode.DOWN
}. Note that this is the rounding mode commonly taught at school.
例子:
input=5.5 output=6
input=2.5 output=3
input=1.6 output=2
input=1.1 output=1
input=1.0 output=1
input=-1.0 output=-1
input=-1.1 output=-1
input=-1.6 output=-2
input=-2.5 output=-3
input=-5.5 output=-6
- 圆形
Rounding mode to round towards the {@literal "nearest neighbor"} unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for {@code
RoundingMode.HALF_UP
} if the digit to the left of the discarded fraction is odd; behaves as for {@codeRoundingMode.HALF_DOWN
} if it's even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as {@literal "Banker's rounding,"} and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for {@code float} and {@code double} arithmetic in Java.
例子
input=5.5 output=6
input=2.5 output=2
input=1.6 output=2
input=1.1 output=1
input=1.0 output=1
input=-1.0 output=-1
input=-1.1 output=-1
input=-1.6 output=-2
input=-2.5 output=-2
input=-5.5 output=-6
关于scala - spark sql圆圆的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62373334/