cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

dataframe - cast string to decimal when encountering zeros returns OE-16

shan_chandra
Databricks Employee
Databricks Employee

The user is trying to cast string to decimal when encountering zeros. The cast function displays the  '0' as '0E-16'. could you please let us know your thoughts on whether 0s can be displayed as 0s?

from pyspark.sql import functions as F
df = spark.sql("select cast('0' AS decimal(38,16)) as decimal_number union all select cast('1.0000123400000' AS decimal(38,16))")
df2 = df.withColumn("string_column", F.expr("format_number(decimal_number, '0.######################')"))
display(df2)  

Screen Shot 2022-03-09 at 12.13.11 PM

1 ACCEPTED SOLUTION

Accepted Solutions

shan_chandra
Databricks Employee
Databricks Employee

If the scale of decimal type is greater than 6, scientific notation kicks in hence seeing 0E-16.

This behavior is described in the existing OSS spark issue - https://issues.apache.org/jira/browse/SPARK-25177

Kindly cast the column to a decimal type less than or equal to 6 to have zeros displayed as zeros.

from pyspark.sql import functions as F
df = spark.sql("select cast('0' AS decimal(10,6)) as decimal_number union all select cast('1.0000123400000' AS decimal(4,2))")
df2 = df.withColumn("string_column", F.expr("format_number(decimal_number, '0.######################')"))
display(df2)  

View solution in original post

1 REPLY 1

shan_chandra
Databricks Employee
Databricks Employee

If the scale of decimal type is greater than 6, scientific notation kicks in hence seeing 0E-16.

This behavior is described in the existing OSS spark issue - https://issues.apache.org/jira/browse/SPARK-25177

Kindly cast the column to a decimal type less than or equal to 6 to have zeros displayed as zeros.

from pyspark.sql import functions as F
df = spark.sql("select cast('0' AS decimal(10,6)) as decimal_number union all select cast('1.0000123400000' AS decimal(4,2))")
df2 = df.withColumn("string_column", F.expr("format_number(decimal_number, '0.######################')"))
display(df2)  

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group