Link to home
Start Free TrialLog in
Avatar of xbox360dp

asked on

Rounding down to the nearest 5

I need some help rounding down to the nearest 5.          
r-price = 100.00                                          
s-price = 42.00                                          
percent saved % = 58                                      
dollar amount saved = 58.00                              
r-price = 100.00                                          
s-price = 42.00                                          
percent saved % = 55                                      
dollar amount saved = 55.00                              
So if the value is 51 - 54 round to 50 and 56-59 round 55.

How do I accomplish this in my trigger?

create or replace                                                                            
TRIGGER "SALE_CALCULATIONS" BEFORE                                                            
  INSERT OR                                                                                  
  UPDATE ON Prs FOR EACH Row                                                                  
:New.Prsvf17c := (:New.Prsprsc - :new.prsvf1c);                            
:New.Prsvf11c := ((:New.Prsprsc - :new.prsvf1c)/:new.prsprsc *100)||'%';
Avatar of pjevin
Flag of United States of America image

Avatar of Sean Stuber
Sean Stuber

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)


I found that with Google as well.
However,  59 gives you 60 not 55.
Ah, sorry I missed the rounding down part, thought it was just the nearest 5.. just subtract 2 first.

2.5 if you have a possibility of decimals, but ceil or floor or whatever you want works too, just not compatible with all databases.
>>> just not compatible with all databases.

well, this was asked in the Oracle topic area, but even if we extend to other platforms, out of curiosity, which ones were you thinking of that don't have a ceiling function or equivalent?
It had a "SQL Query Syntax" tag as well... Oracle didn't in early versions, and most other DBs until recently (Informix did not until 11 which is what I was thinking of specifically, and surprisingly even MS Access).  Back in the day, people used to have to do math!
Avatar of xbox360dp



Thanks for the feedback ... but how do I incorporate your examples in the trigger I have in my question?
what column do you need rounded?

put something like this in your trigger where xxxxx is the column you need rounded

:new.xxxxxx := ceil((:New.xxxxx-4)/5)*5 ;
So ..

:New.Prsvf19c := (ceil(((:new.prsprsc - :new.prsvf1c-4) / :new.prsprsc * 100)  / 5) * 5) || '%'; ??
if you say so

this is the first time you've posted anything about the Prsvf19c  column.

what value will have 58 that you need rounded down to 55?

that's the one you need to put in the xxxxxx  here.

 ceil((xxxxxx-4)/5)*5 ;

then assign that to whatever you column it belongs in,  with '%' appended if you want.

basically, the math is already defined for you.
but only only can decide which columns that math applies to