round away from zero

BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE     11.2.0.1.0     Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
I've been commissioned to write a function to round away from zero
I wish it were the same as round oracle
  round(n , integer ) 
  http://www.di.unipi.it/~ghelli/didattica/bdldoc/B19306_01/server.102/b14200/functions135.htm
with the following exceptions are used if there is no value for the second parameter (integer) default value 2
because it's usually for pennies

the other exception is that it rounds of 0 round_away_from_zero (2.351) = 2.36 and round_away_from_zero (2.32,1) = 2.4

I'm close, but didn't quite.
create or replace function round_away_from_zero   (v_n number, v_integer pls_integer default 2) return number as
begin
     case mod(v_n,.1*(v_integer - 1)) when 0 then
       return trunc(v_n,v_integer);
      else 
      return trunc(v_n,v_integer)+ power(.1,v_integer);
      end case;
        

end; 
Here's my current incorrect results
with t as ( select 1.231 nbr from dual union
                select 1.101   from dual union
                select 1.600 from dual union
                select 1.423 from dual union
                select .30 from dual union
                select .9 from dual union
                select 1.0001 from dual union
                select 10 from dual
             )
select nbr,  round_away_from_zero(nbr,0),  round_away_from_zero(nbr,1), round_away_from_zero(nbr,2)
from t;
NBR     ROUND_AWAY_FROM_ZERO(NBR,0)     ROUND_AWAY_FROM_ZERO(NBR,1)     ROUND_AWAY_FROM_ZERO(NBR,2)
0.3     0     0.4     0.3
0.9     0     1     0.9
1.0001     2     1.1     1.01
1.101     2     1.2     1.11
1.231     2     1.3     1.24
1.423     2     1.5     1.43
1.6     1     1.7     1.6
10     10     10.1     10
Here is my expected results
NBR     ROUND_AWAY_FROM_ZERO(NBR,0)     ROUND_AWAY_FROM_ZERO(NBR,1)     ROUND_AWAY_FROM_ZERO(NBR,2)
0.3     1     0.3     0.3
0.9     1     0.9     0.9
1.0001     2     1.1     1.01
1.101     2     1.2     1.11
1.231     2     1.3     1.24
1.423     2     1.5     1.43
1.6     2     1.7     1.6
10     10     10     10

Ah--could help if that was not in your test case (which was very handy btw)!
Just change the function to test a negative signal, if so, multiply by-1, appeal of ceiling and multiply and then back to the negative. that should do the trick.

PS the suggestion of Frank below to use the SIGN and the ABS is much more elegant, don't know how I didn't have it after the bad things I used to do with SIGN and DECODE pre CASE days statement!

Published by: Carlovski on November 29, 2010 18:51

Tags: Database

Similar Questions

Maybe you are looking for

  • Firefox is compatible with wince6

    HelloI installed a Radio S100 Roadnav system in my car. There the Wince 6.0 operating system. He has touchscreen and Opera 5.1 for surfing the internet. I'm not at all happy with Opera 5.1 and don't want to use IE.Is there a version of Firefox that i

  • After RAM upgrade kernel panic

    IMac 21.5 mid 2010 2 x Kingston 8 GB RAM Anonymous UUID: 48A5979F-AE09-EDC6-A083-98F4ABD9B696 Mon May 23 14:56:23 2016 Panic report *. panic (cpu 1 0xffffff8015fb363a appellant): ' link element invalid queue for 0xffffff802a8df540: 0xffffff803673b3d0

  • Location does not show in the Photos showing each year

    In my app Photos a year, it will also show the summary of the situation (like my photo in 2014) But in my photos from 2015, it shows nothing about location (see image2). But when I Zoom, I see all location information (see photo 3) Anyone know how to

  • KLV-40ex430 HDMI - required Firmware?

    My HDMI with DSTV does not work - I think it's a problem of handshake, but if I use the HDMI on other he sees work 100% I recon I need to upgrade the firmware - I have the current version is of 1,110, but I can't find a version update on the web?

  • Yoga, 13 screens darkens when unplugged

    I had only my Yoga for a few days and love it. I noticed that when I unplug it the screen darkens. I have normal, predictable courthouse or a sign of a problem? Thank you