Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Rounding to 2nd decimal place
Ron
#1 Posted : Friday, July 17, 2009 11:59:15 AM(UTC)
Rank: Member
Ron
Groups: Member

Joined: 9/4/2008(UTC)
Posts: 199
Points: 398
Hi,
I need to round to the second decimal place (AwayFromZero method):
Convert 2.565 to 2.57

Attempts that did not work:
ROUND(number,2)
CAST(number as Decimal(18,2))

Looks like I could use STR(number, 33, 2) and then CAST it back to Decimal, but I'm wondering if there is a better way. Also, does anyone know what the maximum number of characters a decimal field could create (including '.' and '-' characters (I just guessed at 33)?

thanks,



Ron
David McCallum
#2 Posted : Friday, July 17, 2009 8:38:48 PM(UTC)
Rank: Team VistaDB
davidmccallum
Groups: Member, Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 696
Points: 2,916
Location: Edinburgh, Scotland
Use ROUND(number, 2)
Ron
#3 Posted : Saturday, July 18, 2009 5:10:48 AM(UTC)
Rank: Member
Ron
Groups: Member

Joined: 9/4/2008(UTC)
Posts: 199
Points: 398
Hi David,

Ops, I fixed above to include ",2". ROUND(number,2) does not work for rounding 2.565 to 2.57 (in VistaDB).

Ron
David McCallum
#4 Posted : Saturday, July 18, 2009 5:52:24 AM(UTC)
Rank: Team VistaDB
davidmccallum
Groups: Member, Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 696
Points: 2,916
Location: Edinburgh, Scotland
On my version it does (3.6.86.1)


js_vistadb
#5 Posted : Saturday, July 18, 2009 9:16:46 AM(UTC)
Rank: Team VistaDB
js_vistadb
Groups: Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 3,492
Points: 5,124
Location: Seattle, Washington
What are you seeing Ron?

I am wondering if the locale of your database would impact that? I don't think so. I can only think of strings, date times, money, I don't think things like round would use anything locale specific.

Jason Short

Ron
#6 Posted : Saturday, July 18, 2009 10:41:16 AM(UTC)
Rank: Member
Ron
Groups: Member

Joined: 9/4/2008(UTC)
Posts: 199
Points: 398
Hi David,
In your sample, you are using the wrong number (2.567).

I need:
Round 2.565 to 2.57


thanks
Ron
js_vistadb
#7 Posted : Saturday, July 18, 2009 11:21:53 AM(UTC)
Rank: Team VistaDB
js_vistadb
Groups: Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 3,492
Points: 5,124
Location: Seattle, Washington
No, that won't because of the issues we have already talked about (differences in default rounding in .Net compared to VC++ used by SQL Server).

You will have to write custom code to do that if that is the behavior you want. Put it in a CLR Proc if you want it callable from the database, or in your business logic.

Jason Short

David McCallum
#8 Posted : Saturday, July 18, 2009 9:21:46 PM(UTC)
Rank: Team VistaDB
davidmccallum
Groups: Member, Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 696
Points: 2,916
Location: Edinburgh, Scotland
Oops slip of the finger.

In that case add 0.001 to the initial number, that way every thing from 2.560-2.564 will round down to 2.56 and everything from 2.565-2.569 will round up to 2.57
Ron
#9 Posted : Sunday, July 19, 2009 7:39:25 AM(UTC)
Rank: Member
Ron
Groups: Member

Joined: 9/4/2008(UTC)
Posts: 199
Points: 398
Hi David,
With "To Even" (or "Bankers" rounding), it's rounded towards the nearest even whole number.
2.565 = 2.56
2.675 = 2.58
so it's not always rounded down with x.xx5

Ron
David McCallum
#10 Posted : Sunday, July 19, 2009 9:03:43 AM(UTC)
Rank: Team VistaDB
davidmccallum
Groups: Member, Team VistaDB

Joined: 8/13/2006(UTC)
Posts: 696
Points: 2,916
Location: Edinburgh, Scotland
OK, See Jason's last answer
nsmith
#11 Posted : Tuesday, July 21, 2009 11:10:22 AM(UTC)
Rank: Team VistaDB
nsmith
Groups: Member, Team VistaDB

Joined: 4/3/2009(UTC)
Posts: 98
Points: 100
Location: Oklahoma City
Here is a TSQL round function for you:

CREATE FUNCTION [MyRound]
(@Operand Decimal,@Places Int)
RETURNS Decimal
AS
BEGIN
declare @x decimal
declare @i int

set @x = @Operand * power(10,@Places)
set @i = @x
set @x = @i + iif((@x - @i) >= .5,1,0)
set @x = @x / power(10,@Places)
Return(@x)
END
Ron
#12 Posted : Tuesday, July 21, 2009 4:38:48 PM(UTC)
Rank: Member
Ron
Groups: Member

Joined: 9/4/2008(UTC)
Posts: 199
Points: 398
thanks nsmith, I'll give it a try.

Ron
nsmith
#13 Posted : Wednesday, July 22, 2009 5:44:17 AM(UTC)
Rank: Team VistaDB
nsmith
Groups: Member, Team VistaDB

Joined: 4/3/2009(UTC)
Posts: 98
Points: 100
Location: Oklahoma City
Oops I had a bug. See revision above.
Sorry,
Nick
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.