How to remove negative symbol for numeric value using spark sql command?

Viewed 29

How to remove negative symbol for numeric value using spark sql command?

1 Answers

format_number can be used to control the output of numeric columns. And following this idea we can omit the minus sign for negative values:

from pyspark.sql import functions as f

df=spark.createDataFrame([[1.1],[-2.2]],['col'])
df.withColumn("formatted", f.expr("format_number(col, '0.00###; 0.00###')")).show()

Output:

+----+---------+
| col|formatted|
+----+---------+
| 1.1|     1.10|
|-2.2|     2.20|
+----+---------+

In a plain SQL environment (without PySpark)

select col, format_number(col, '0.00###; 0.00###') from <tablename>

returns the same result.

Related