玉祥平台客服-15087858732

标题: REMAINDER函数:四舍六入五取偶 [打印本页]

作者: 张兴康    时间: 2020-2-3 15:07
标题: REMAINDER函数:四舍六入五取偶
REMAINDER m-(m/n)*n ; (m/n)4舍6入,5取偶。当(m/n)为x.5的时候,取最接近m/n的偶数

下面为验证代码:
declare
  num1 number;
  num2 number:=&4;
  out1 number:=1;
  out2 number:=3;
  i    number;
  erro exception;
begin
  i:=1;
  while i<=10000 loop
    i:=i+1;
    num1:=i;
    SELECT remainder(num1, num2) into out1
      FROM duaL;

    select num1 - (case
             when substr(col, instr(col, '.')) = '.5' and
                  mod(floor(col), 2) = 0 then
              floor(col)
             else
              round(col)
           end) * num2 into out2
      from (select num1 / num2 col from dual);

    if  nvl(out1,'1')<>nvl(out2,'2') then
      raise erro;
    end if;

  end loop;
  DBMS_OUTPUT.put_line ( num1||'-'||num2||'-'||out1||'-'||out2);
exception
  when erro then

   raise_application_error(-20000,num1||'-'||num2||'-'||out1||'-'||out2,TRUE);
   RAISE;
  when others then
   raise_application_error(-20000,num1||'-'||num2||'-'||out1||'-'||out2,TRUE);
   RAISE;
end;


作者: 张兴康    时间: 2020-2-3 15:08
听说彩票计算奖金就是这种算法,为的是,总值更接近于真实值




欢迎光临 玉祥平台客服-15087858732 (http://bbs.delit.cn/) Powered by Discuz! X3.2