×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Rounding to nearest whole number

## Rounding to nearest whole number

(OP)

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??

### RE: Rounding to nearest whole number

Does something like CAST((value + 0.50) AS INTEGER) give you the results you would like ?

### RE: Rounding to nearest whole number

(OP)
That did the trick.

### RE: Rounding to nearest whole number

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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!