Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 round

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-08-20 : 09:49:02
Hi,
I am rounding numbers to 2 decimal places.
How is it possible to get the 0 at the end also?
For example, rounding 8.1243 gives 8.12
But rounding 6.4 gives 6.4 whereas I would like to see 6.40
OR if the answer is say 2 I would like to see 2.00
How is this done please?
Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-20 : 10:05:04
Use DECIMAL. That would round it as well by default.
DECLARE @x FLOAT = 2.0;

SELECT ROUND (@x,2);
SELECT CAST(@x AS DECIMAL(19,2))
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-08-20 : 13:26:28
Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-08-20 : 13:53:40
Just to add to what Sunitbeck said, if you pass in a numeric/decimal, you'll get that same datatype back out.
DECLARE @x FLOAT = 2.0;
SELECT ROUND (CAST(@x AS DECIMAL(19, 2)), 2);

DECLARE @y DECIMAL(19, 2) = 2.0;
SELECT ROUND (@y, 2);
But, then there isn't really a need to round, assuming the datatype has the precision you want:
DECLARE @z DECIMAL(19, 2) = 2.12345;
SELECT @z
Go to Top of Page
   

- Advertisement -