Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to nearest whole number

Status
Not open for further replies.
Feb 20, 2006
5
IE

Hi,
I'm trying to round a decimal(18,2) to the nearest whole number (<=.49 should round down, >=.50 should round up).

I've tried casting as decimal(18,0) but this rounds 1.50 up and 2.50 down (odd numbers round up, even numbers round down).

casting as integer just truncates the last 2 digits.

How can I do it so that .50 always goes up??
 
Does something like CAST((value + 0.50) AS INTEGER) give you the results you would like ?
 
Be careful, if there are negative values (wrong result) or values outside of the range of an integer (error).

The default rounding in Teradata (and most programming languages) is banker's rounding, but you can switch to your desired behaviour (commercial rounding?), there's a global flag controlling it:

A dba can set it using dbscontrol, it's General field 20. RoundHalfwayMagUp

But it's global, so maybe your company can't change it...

For details check:
SQL Reference: Data Types and Literals
Chapter 3: Decimal/Numeric Data Types
Rounding

Another solution would probably be a UDF.

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top