# Copyright (c) 2009 bivio Software Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Bytes.pm,v 2.1 2009/04/23 17:16:49 moeller Exp $ package Bivio::UI::HTML::Format::Bytes; use strict; use Bivio::Base 'Bivio::UI::HTML::Format'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.1 $ =~ /\d+/g); sub get_widget_value { my($self, $size) = @_; return '' unless defined($size) && length($size); return '0 KB' if $size == 0; $size = _format($size, "%.0f"); return '1 KB' unless $size; # less than 3 digits return $size . ' KB' if $size < 1000; return _format($size, "%.1f") . ' MB'; } sub _format { my($v, $f) = @_; $v = sprintf($f, $v / 1024.0); $v =~ s/\.0$//; return $v; } 1;