extracting decimal part of a number or the integer part?

extracting decimal part of a number or the integer part?

Post by Umlja » Sat, 16 Aug 2008 00:12:00


declare @m money
set @m = 123.67
select @m - cast(@m as int)
select cast(@m as int) - @m

one returns -0.33
and the other returns 0.33

obviously from rounding. How can I extract the decimal or the integer part
without rounding?

Thanks,
Rich
 
 
 

extracting decimal part of a number or the integer part?

Post by SQL Server » Sat, 16 Aug 2008 00:22:33

On Thu, 14 Aug 2008 08:12:00 -0700, Rich



I don't see what rounding has to do with what is returned by the
sample code. The sign changes because the terms are reversed in the
subtraction, but the absolute values of the two are identical.

Perhaps you can elaborate?

Roy Harvey
Beacon Falls, CT

 
 
 

extracting decimal part of a number or the integer part?

Post by --CELKO- » Sat, 16 Aug 2008 00:26:34

>> How can I extract the decimal or the integer part without rounding? <<

1) Stop using MONEY; it has math problems and is proprietary. Use
DECIMAL(s,p) to the proper number of places that your accounting
department needs.

2) One way is to use the FLOOR function
FLOOR (123.67) = 123
likewise:
123.67 - FLOOR (123.67)
123.67 - 123
0.67
 
 
 

extracting decimal part of a number or the integer part?

Post by Umlja » Sat, 16 Aug 2008 02:13:01

Sorry about my ambiguity.

if a number is 123.45 -- cast(123.45 as int) will return 123

if a number is 123.75 -- cast(123.75 as int) will return 124

The FLOOR function will extract the int portion of the number.

Thank you all for your replies and help.