INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Jobs

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)
Thanks for your help Al.
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close