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:ย 

How to no round formating

William_Scardua
Valued Contributor

Hy guys,

I need to format the decimal values but I can`t round then

Screenshot 2024-03-20 at 10.01.52.png

have any idea ?

thank you

 

โ€ƒ

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz_Fatma
Community Manager
Community Manager

Hi @William_ScarduaIn Databricks, you can format decimal values without rounding them using a couple of approaches. Letโ€™s explore some options:

  1. Using substring: You can use the substring function to extract a specific number of decimal places from your decimal value. For example:

    spark.sql("SELECT SUBSTRING(12.455555, 0, INSTR(12.455555, '.') + 2) AS truncated_value").show()
    

    This will give you the result: 12.451.

  2. Custom Formatting with format_number: The format_number function allows you to override the default rounding format. You can specify the desired number of decimal places. For instance:

    spark.sql("SELECT FORMAT_NUMBER(12.455555, 2) AS formatted_value").show()
    

    This will display: 12.462.

Remember that these solutions will trim the decimal places without rounding off the values. Choose the one that best fits your requirements! ๐Ÿš€

 

View solution in original post

1 REPLY 1

Kaniz_Fatma
Community Manager
Community Manager

Hi @William_ScarduaIn Databricks, you can format decimal values without rounding them using a couple of approaches. Letโ€™s explore some options:

  1. Using substring: You can use the substring function to extract a specific number of decimal places from your decimal value. For example:

    spark.sql("SELECT SUBSTRING(12.455555, 0, INSTR(12.455555, '.') + 2) AS truncated_value").show()
    

    This will give you the result: 12.451.

  2. Custom Formatting with format_number: The format_number function allows you to override the default rounding format. You can specify the desired number of decimal places. For instance:

    spark.sql("SELECT FORMAT_NUMBER(12.455555, 2) AS formatted_value").show()
    

    This will display: 12.462.

Remember that these solutions will trim the decimal places without rounding off the values. Choose the one that best fits your requirements! ๐Ÿš€

 
Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!