]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - config/cfgroot/general-functions.pl
general-functions.pl: Add formatBytes() function.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / general-functions.pl
index 41a0eac2d43bd3a007ae79adc68c99f99b0279a5..692e072c2ac9e439ebfbc54fddbc5d07b27e0c60 100644 (file)
@@ -1261,4 +1261,29 @@ sub get_nameservers () {
        return &uniq(@nameservers);
 }
 
+# Function to format a string containing the amount of bytes to
+# something human-readable. 
+sub formatBytes {
+       # Private array which contains the units.
+       my @units = qw(B KB MB GB TB PB);
+
+       my $bytes = shift;
+       my $unit;
+
+       # Loop through the array of units.
+       foreach my $element (@units) {
+               # Break loop if the bytes are less than the next unit.
+               last if $bytes < 1024;
+
+               # Divide bytes amount with 1024.
+               $bytes /= 1024;
+
+               # Assign current processed element to unit.
+               $unit = $element;
+       }
+
+       # Return the divided and rounded bytes count and the unit.
+       return sprintf("%.2f %s", $bytes, $unit);
+}
+
 1;