OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

How to round a number to a given multiplier

September 6th, 2006 By Eddie Awad

The following query rounds a number up to the next multiplier.

Assumptions: num can be any positive number. multiplier can be any positive integer.

SQL> var num number;
SQL> var multiplier number;
SQL> exec :num := 19; :multiplier := 20;

PL/SQL procedure successfully completed.

SQL> SELECT   TRUNC (:num / :multiplier) * :multiplier
  2         + DECODE (MOD (:num, :multiplier), 0, 0, :multiplier) r
  3    FROM DUAL;

         R
----------
        20

And the following query does the opposite, it rounds down:

SQL> exec :num := 45; :multiplier := 40;

PL/SQL procedure successfully completed.

SQL> SELECT TRUNC (:num / :multiplier) * :multiplier r
  2    FROM DUAL;

         R
----------
        40

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question