Question : Why is my SQL SELECT calculation completing the decimal places to the right?

I am trying to get a table-valued function to return a percentage.  

I've tried both a numeric type and a decimal with different results.

Boiled down this is my calculation:

DECLARE @CompletionPercent NUMERIC(18,8)
--DECLARE @CompletionPercent DECIMAL
Set @CompletionPercent =
(
      (
            (3*100000000)
            /  9
      )
)
SELECT  @CompletionPercent / 1000000


The result is this:
33.3333330000000000

Notice the zeros at the end.  I'd like to get the calculation to be carried out farther.

Thanks!

Answer : Why is my SQL SELECT calculation completing the decimal places to the right?

@aneeshattingal,

Actually, he is trying to get more decimal places of accuracy.  

@cylikon,

The problem is that you are working with integer arithmetic and you aer getting only as many deciaml places as the number of zeros behind the "3" that you are dividing by 9.

You first multiple the 3 by 100000000 (which has 8 zeros following the 1), then you divide by 9, which gives you 33333333 (notice, 8 3's).  That valu is what your @CompletionPercent is set to.

Then you divide the @CompletionPercent by 1000000 (notice, 6 zeros) which gives you the 33 and six 3's following it.

What you probably should do is to multiply your 3 (or whatever value) by 100 and then divide by 9.0, which will make cause the calculation to not be done as integer arithmenic and that will give you the calculated percentage in the format you are trying to achieve.

Random Solutions  
 
programming4us programming4us