Solved

# How to solve decimal error during math operations in t-sql?

Posted on 2008-11-13
312 Views
Hi

I have a quite complex query which is not behaving as I want it to.
I have boiled it down to a small example that shows my problem:

Select 30.94/0.178*0.178

it returns 30.939999872
I want it to return 30.94  or 30.94000000000

Can I do some kind of conversion to other datatypes in the select statement orwhat should I do. I need it to be able to do this kind of calculation without making decimal errors.

0
Question by:liversen

LVL 60

Expert Comment

Select cast((30.94/0.178*0.178) as decimal(18,2))
0

LVL 8

Accepted Solution

Unless I am missing something you do not need to cast or convert. You can use round().
SELECT ROUND(123.4545, 2) returns  123.4500

And thus you need:
Select  ROUND(30.94/0.178*0.178, 2)
0

LVL 60

Expert Comment

>>Unless I am missing something you do not need to cast or convert. You can use round().

Round is doing the cast for you.  :)
0

LVL 8

Expert Comment

chapmandew :)
CAST (technically) converts an expression of one data type to another (even if you are converting between the same types)
ROUND() is mathematical function.
0

LVL 60

Expert Comment

;)
0

LVL 1

Author Comment

Thanks. That solves it for my simple example unfortunately if I add a bit more complexity I can't get it to work:
Here's another example which again is simplyfied a bit:

SELECT ROUND((ROUND((30.94-(30.94-100000))/0.178,0,1)*0.178)+(30.94-100000),3)
I want it to return 30.94. It does not. Instead it returns 30.806000000

I have tried to put this CAST function on it. It does not change anything  :o(
I must admit that I do not have the full overview of the round function.

Any help is very much appreciated.

0

LVL 8

Expert Comment

Why are you nesting round(round())? This is causing wrong result.
Here:
SELECT top 1 ROUND((30.94-(30.94-100000)/0.178*0.178)+(30.94-100000),3)  from login
0

LVL 8

Assisted Solution

Opps
rounded to the wrong digit. You need second, right?
ROUND((30.94-(30.94-100000)/0.178*0.178)+(30.94-100000),2)
0

LVL 1

Author Comment

I round twice because I'm also grouping by that column in the complete query  and I want to group in intervals of 0.178 size.
Anyway I think I have fixed it by removing the 100000 offset. So now it seems to work.

Thanks to all of you for your efforts
0

LVL 8

Expert Comment

but if you nest your rounds you are doomed to get your calculations wrong. Even if you round down to the same digit. Even simple a+b will give you difference with nesting functions. Here is simplified example:
select round((5.54*5.54),2)  as simple, round(5.54*round(5.4,2),2) as nested_rnd

With small numbers it might be not as obvious, but it's just matter of meeting certain condition when error will pop up.
0

## Featured Post

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.