PL/SQL格式化字節

create or replace function format_bytes(pi_bytes in number) return varchar2
is
  c_unit constant number := 1024;
  l_size number;
  l_unit_ind integer;
begin
  l_size := pi_bytes;
  l_unit_ind := 0;
  while l_size > c_unit loop
    l_size := l_size / c_unit;
    l_unit_ind := l_unit_ind + 1;
  end loop;
  case
    when l_unit_ind = 0 then
      return l_size||'B';
    when l_unit_ind = 1 then
      return round(l_size, 2)||'KB';
    when l_unit_ind = 2 then
      return round(l_size, 2)||'MB';
    when l_unit_ind = 3 then
      return round(l_size, 2)||'GB';
    when l_unit_ind = 4 then
      return round(l_size, 2)||'TB';
    when l_unit_ind = 5 then
      return round(l_size, 2)||'PB';
    else
      return pi_bytes||'B';
  end case;
exception
  when others then
    return pi_bytes||'B';
end format_bytes;
/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章