dbTalk Databases Forums  

How to round down decimal like 15.9 to 15?

microsoft.public.sqlserver.server microsoft.public.sqlserver.server


Discuss How to round down decimal like 15.9 to 15? in the microsoft.public.sqlserver.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andreas Klemt
 
Posts: n/a

Default How to round down decimal like 15.9 to 15? - 01-20-2009 , 03:48 PM






Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas

Reply With Quote
  #2  
Old   
Russell Fields
 
Posts: n/a

Default Re: How to round down decimal like 15.9 to 15? - 01-20-2009 , 03:57 PM






SELECT FLOOR(@myvar)

RLF

"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote

Quote:
Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas


Reply With Quote
  #3  
Old   
Tom Cooper
 
Posts: n/a

Default Re: How to round down decimal like 15.9 to 15? - 01-20-2009 , 03:58 PM



Always give a precision and scale when declaring decimal datatypes. If you
don't, it defaults to decimal(18,0) which can only hold integers. So when
you put 15.84 into a decimal (18,0) it's immediately converted to 16. And,
of course, whatever type of rounding you do to 16 will get you 16.

But you want the Round() function. See BOL for a description. Something
like

declare @myvar as decimal (5,2)
set @myvar=15.84
select Cast(Round(@myvar, 0, 1) As int)

which returns 15

Tom

"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote

Quote:
Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas



Reply With Quote
  #4  
Old   
Tibor Karaszi
 
Posts: n/a

Default Re: How to round down decimal like 15.9 to 15? - 01-20-2009 , 04:05 PM



It (CAST) does round down (truncates). Your problem was that you
define the decimal as decimal, which is same as decimal(18,0) which
means that you will get a round up, at the assignment stage, not the
CAST. Anyhow, you can also use FLOOR. See below, should explain it
all:

declare @myvar1 as decimal, @myvar2 as decimal(5,2)
set @myvar1=15.84
set @myvar2=15.84
SELECT @myvar1, @myvar2
select cast(@myvar1 as int), cast(@myvar2 as int)
SELECT FLOOR(@myvar1), FLOOR(@myvar2)

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote

Quote:
Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas



Reply With Quote
  #5  
Old   
Andreas Klemt
 
Posts: n/a

Default Re: How to round down decimal like 15.9 to 15? - 01-20-2009 , 05:43 PM



Thanks a lot! That helped me.

Andreas



"Tibor Karaszi" <tibor_please.no.email_karaszi (AT) hotmail (DOT) nomail.com> schrieb
im Newsbeitrag news:%23BTG8s0eJHA.5316 (AT) TK2MSFTNGP06 (DOT) phx.gbl...
Quote:
It (CAST) does round down (truncates). Your problem was that you define
the decimal as decimal, which is same as decimal(18,0) which means that
you will get a round up, at the assignment stage, not the CAST. Anyhow,
you can also use FLOOR. See below, should explain it all:

declare @myvar1 as decimal, @myvar2 as decimal(5,2)
set @myvar1=15.84
set @myvar2=15.84
SELECT @myvar1, @myvar2
select cast(@myvar1 as int), cast(@myvar2 as int)
SELECT FLOOR(@myvar1), FLOOR(@myvar2)

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message
news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas




Reply With Quote
  #6  
Old   
Jon Ford
 
Posts: n/a

Default Re: How to round down decimal like 15.9 to 15? - 07-20-2011 , 08:46 AM



Hi Andreas

Have you considered using either the Fix or Floor functions on the report - either of these will return the integer part of the number i.e. 15

Jon

Quote:
On Tuesday, January 20, 2009 4:48 PM Andreas Klemt wrote:

Hello,

I have this:

declare @myvar as decimal
set @myvar=15.84
select cast(@myvar as int)
Result: 16

I want to always round down. How can I get a result 15?

Thanks for any help in advance!

Andreas

Quote:
On Tuesday, January 20, 2009 4:57 PM Russell Fields wrote:

SELECT FLOOR(@myvar)

RLF

Quote:
On Tuesday, January 20, 2009 4:58 PM Tom Cooper wrote:

Always give a precision and scale when declaring decimal datatypes. If you
don't, it defaults to decimal(18,0) which can only hold integers. So when
you put 15.84 into a decimal (18,0) it's immediately converted to 16. And,
of course, whatever type of rounding you do to 16 will get you 16.

But you want the Round() function. See BOL for a description. Something
like

declare @myvar as decimal (5,2)
set @myvar=15.84
select Cast(Round(@myvar, 0, 1) As int)

which returns 15

Tom

"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message
news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl...

Quote:
On Tuesday, January 20, 2009 5:05 PM Tibor Karaszi wrote:

It (CAST) does round down (truncates). Your problem was that you
define the decimal as decimal, which is same as decimal(18,0) which
means that you will get a round up, at the assignment stage, not the
CAST. Anyhow, you can also use FLOOR. See below, should explain it
all:

declare @myvar1 as decimal, @myvar2 as decimal(5,2)
set @myvar1=15.84
set @myvar2=15.84
SELECT @myvar1, @myvar2
select cast(@myvar1 as int), cast(@myvar2 as int)
SELECT FLOOR(@myvar1), FLOOR(@myvar2)

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


"Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message
news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl...

Quote:
On Tuesday, January 20, 2009 6:43 PM Andreas Klemt wrote:

Thanks a lot! That helped me.

Andreas

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.