SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rounding Decimal value get wrong result


Rounding Decimal value get wrong result

Author
Message
ada.xi
ada.xi
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
hi all,

I try to give a expression to round some filed like below,
Round(Fields!unit_price.Value, Fields!Dom_Price_Places.Value)
---- Fields!unit_price.Value(decimal(18,8))
----Fields!Dom_Price_Places.Value(tinyint)
now I run into a one Fields!unit_price.Value=2.34500000, Fields!Dom_Price_Places.Value=2
What I get is
Round(CDec("2.34500000"), 2) return 2.34.

Round(2.34500000, 2) return 2.35

What's the difference here?
Is this a bug?

And help would be appreciated. Thanks,
ada.xi
ada.xi
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
Any Help? thanks
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167425 Visits: 39480
ada.xi (1/25/2011)
hi all,

I try to give a expression to round some filed like below,
Round(Fields!unit_price.Value, Fields!Dom_Price_Places.Value)
---- Fields!unit_price.Value(decimal(18,8))
----Fields!Dom_Price_Places.Value(tinyint)
now I run into a one Fields!unit_price.Value=2.34500000, Fields!Dom_Price_Places.Value=2
What I get is
Round(CDec("2.34500000"), 2) return 2.34.

Round(2.34500000, 2) return 2.35

What's the difference here?
Is this a bug?

And help would be appreciated. Thanks,




What is CDec? I can't find it in BOL anywhere.

My guess, CDec is converting "2.34500000" to 2.34, not 2.345.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ada.xi
ada.xi
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
Cdec ---Convert to Decimal.

And (CDec("2.34500000")=2.34500000) return true.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167425 Visits: 39480
ada.xi (1/25/2011)
Cdec ---Convert to Decimal.



In what? As I said, I can't find it anywhere in Books Online.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167425 Visits: 39480
Lynn Pettis (1/25/2011)
ada.xi (1/25/2011)
Cdec ---Convert to Decimal.



In what? As I said, I can't find it anywhere in Books Online.


Nevermind, I found it in the Visual Studio Help System, it is a VB function.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Daniel Bowlin
Daniel Bowlin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13524 Visits: 2629
You say that
Round(CDec("2.34500000"), 2) return 2.34.

but it should be
Round(2.34500000, 2) return 2.35


What happens when the number is 2.35500000 does it return
2.35 or 2.36?

If it returns 2.36 you are encountering something called banker's rounding where a 5 will round up to an even number, but a 5 will round down if the round up number is odd. In other words the 5 will alway round in the direction of the even number whether that is up or down.

Without the long explanation of why banker's rounding exists, most people just code around it by testing the digit they are rounding and if it is a 5 they will add a value of 1 to that digit to make sure it rounds up.

Hope this helps.
Doug Lane
Doug Lane
SSC Eights!
SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)

Group: General Forum Members
Points: 965 Visits: 14618
Daniel Bowlin (1/27/2011)

If it returns 2.36 you are encountering something called banker's rounding where a 5 will round up to an even number, but a 5 will round down if the round up number is odd. In other words the 5 will alway round in the direction of the even number whether that is up or down.

I think the use of CDec is invoking the bankers rounding somehow. Try dropping that function if you can; maybe put the rounding in the query if that's possible.

For what it's worth, here's something more from MSDN:
When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0 and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in.

Though it doesn't include CDec in that list, I wouldn't be surprised if it behaves like CInt and CLng.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search