How to deal with Decimal data type arithmetic operations ?

Sai Pavan Batchu 1 Reputation point
2022-08-13T19:51:38.297+00:00

Hello all

I am dealing with values ranging from 10^9 to 10^-9 , the sum of values can go up to 10^20 and need accuracy.

So I wanted to use Decimal Data type [ Using SQL in Data Science & Engineering workspace].

However, I got to know the peculiar behavior of Decimal Data Type. That is, to change the precision to preserve accuracy.

Decimal Precision Scale

Decimal Precision Loss

Due to this behavior, I am facing issues while multiplying or dividing numbers. [I have a case of series of multiplications and divisions for transforming data]

When I multiply two numbers, the precision changes and when multiply again, there is a loss of accuracy.

For example

SELECT CAST(1234.456 as decimal(20,8))*CAST(23478.89076 as decimal(20,8))*CAST(345678.125567 as decimal(20,8))  

The answer I am getting : 10019016421573.927554

Real Answer: 10019016421573.92755433905952

I am looking for scale around 8

Is there any specific way to handle this kind of scenario ?

Note: All the numeric columns in my case will have same data type.

This content is a preview of a link.learn.microsoft.com

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,907 questions
{count} votes

1 answer

Sort by: Most helpful
  1. HimanshuSinha-msft 19,376 Reputation points Microsoft Employee
    2022-08-16T20:02:25.317+00:00

    Hello @Sai Pavan Batchu ,
    Thanks for the question and using MS Q&A platform.

    As we understand the ask here is how to get the output of the SQL statement as 10019016421573.92755433905952, please do let us know if its not accurate.
    To me its more as to how to play with the precision value . If you see the all the three numbers which you are trying to multiply have a different precision .

    The below query did worked for me and i did compared to the readl values which is expected and it matches

    SELECT CAST(1234.456 as decimal(20,13))*CAST(23478.89076 as decimal(20,12))*CAST(345678.125567 as decimal(20,13))
    Output : 10019016421573.92755433905952

    Please do let me if you have any queries.
    Thanks
    Himanshu


    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
      • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators