Question : Rounding Down Money or Truncating to two numbers to the right of the decimal

I'm having a rounding issue in SQL Server 2000.  I guess I shouldn't say rounding issue...it's doing exactly what I'm telling it to do...I'm in need of a little tweaking of my code

Let's say I have $23.87 to distribute amoung 3 people with the following breakdown

Person 1 gets 13/22 which equals $14.105
Person 2 gets 3/22  which equals $3.255
Person 3 gets 6/22 which equals 6.510

I'm using this formula
SELECT CAST(23.87 * ((.13*100)/22) AS SMALLMONEY)

At the end of the week I created a report and sum on each persons distribution...the number is greater than what I started with becuase of rounding.
Person 1 gets $14.11
Person 2 gets $3.26
Person 3 gets $6.51

If you add them all up it equals 23.88 (Over by 1 penny).  It may seem insignificant, but when this is done 10's of thousands of times it really starts to add up.

What I would like to do is truncate the down to two numbers to the right of the decimal point in the calculation.  I can then assign the extra penny if there are is one.

I tried ths, but it rounds and then truncates.
SELECT CAST(CAST(23.87 * ((.13*100)/22) AS DECIMAL(10,2)) AS SMALLMONEY)

I need to truncate the data so it looks like this 14.10 and 3.25...bascially, it should always round down.

Can someone please point me in the right direction?

Thanks
Mike

Answer : Rounding Down Money or Truncating to two numbers to the right of the decimal

The basic logic is something like this. You could probably put it into a udf.

DECLARE @Amount FLOAT
SET @Amount = (23.875 * ((.13*100)/22))

SELECT      CAST      (       
                  FLOOR(@Amount) +
                  ( FLOOR( (@Amount - FLOOR(@Amount)) * 100) / 100)
                  AS SMALLMONEY
            )

 

Another option is to use a simple hack. Convert the value to a varchar and then use: CAST(SUBSTRING(TheValue, 1,  CharIndex of decimal point + 2) AS SMALLMONEY)

Random Solutions  
 
programming4us programming4us