Friday, 23 September 2011

Validating a number as an integer

set serveroutput on
declare
   l_Char_Num varchar2 (40) := '&Enter_Number';
   i          number;
begin
  i := to_number (l_Char_Num);
  if ( i = trunc(i) ) then
    dbms_output.put_line (i ||' is an Integer');
  else
    dbms_output.put_line (i ||' is not an Integer');
  end if;
  exception
    when others then
      dbms_output.put_line ('Not an Integer');
end proc;
/
Enter value for Enter_Number: 40.6
old   2:    p_search varchar2 (40) := '&num';
new   2:    p_search varchar2 (40) := '40.6';

40.6 is not an Integer


However, when run with -40, outcome is;

Enter value for Enter_Number: -40
old   2:    p_search varchar2 (40) := '&num';
new   2:    p_search varchar2 (40) := '-40';

-40 is an Integer

No comments:

Post a Comment