Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dataframe spark数据帧中不带小数点的双值舍入_Dataframe_Apache Spark_Apache Spark Sql_Rounding - Fatal编程技术网

Dataframe spark数据帧中不带小数点的双值舍入

Dataframe spark数据帧中不带小数点的双值舍入,dataframe,apache-spark,apache-spark-sql,rounding,Dataframe,Apache Spark,Apache Spark Sql,Rounding,我试图在spark数据帧中舍入一个不带小数点的双精度值,但在输出时得到了相同的值 下面是dataframe列值 +-----+-----+ | SIG1| SIG2| +-----+-----+ | 46.0| 46.0| | 94.0| 46.0| dataframe列的架构如下所示 scala> df.printSchema root |-- SIG1: double (nullable = true) |-- SIG2: double (nullable = true) 预期

我试图在spark数据帧中舍入一个不带小数点的双精度值,但在输出时得到了相同的值

下面是dataframe列值

+-----+-----+
| SIG1| SIG2|
+-----+-----+
| 46.0| 46.0|
| 94.0| 46.0|
dataframe列的架构如下所示

scala> df.printSchema
root
 |-- SIG1: double (nullable = true)
 |-- SIG2: double (nullable = true)
预期输出如下所示

+-----+-----+
| SIG1| SIG2|
+-----+-----+
| 46  |   46|
| 94  |   46|
我已根据文件尝试将以下列四舍五入

+------------------------------------------------------------------+
|ReturnType| Signature     |                            Description|
+------------------------------------------------------------------+
|DOUBLE    |round(DOUBLE a)| Returns the rounded BIGINT value of a.|
使用的代码是

val df1 = df.withColumn("SIG1", round(col("SIG1"))).withColumn("SIG2", round(col("SIG2")))
我们需要将列强制转换为int/bigint,还是可以使用round函数本身


提前谢谢

您不需要强制转换列。如果要去掉小数点后的数字,可以使用
舍入(colName,0)

round
函数也会返回双精度值,所以如果需要int-type,则将其强制转换

scala> Seq(1.9999,2.1234,3.6523).toDF().select(round('value,2)).show()
+---------------+
|round(value, 2)|
+---------------+
|            2.0|
|           2.12|
|           3.65|
+---------------+


scala> Seq(1.9999,2.1234,3.6523).toDF().select(round('value,0)).show()
+---------------+
|round(value, 0)|
+---------------+
|            2.0|
|            2.0|
|            4.0|
+---------------+


scala> Seq(1.9999,2.1234,3.6523).toDF().select(round('value)).show()
+---------------+
|round(value, 0)|
+---------------+
|            2.0|
|            2.0|
|            4.0|
+---------------+

scala> Seq(1.9999,2.1234,3.6523).toDF().select('value.cast("int")).show()
+-----+
|value|
+-----+
|    1|
|    2|
|    3|
+-----+

请不要借此机会否决投票,正如我提到的,我在一份文件中看到,这是可能的,而且不会发生,我只是想确认一下!它是四舍五入的