Skip to content

Instantly share code, notes, and snippets.

@sirtony
Last active August 29, 2015 14:02
Show Gist options
  • Save sirtony/76afd2a07f80a5b60d1f to your computer and use it in GitHub Desktop.
Save sirtony/76afd2a07f80a5b60d1f to your computer and use it in GitHub Desktop.
Utilities for representing arbitrary byte-lengths in a human-readable way.
using System;
public static class Format
{
public static string HumanSize( int size, int places = 2, bool lower = false )
{
return Format.HumanSize( Convert.ToDecimal( size ), places, lower );
}
public static string HumanSize( long size, int places = 2, bool lower = false )
{
return Format.HumanSize( Convert.ToDecimal( size ), places, lower );
}
public static string HumanSize( float size, int places = 2, bool lower = false )
{
return Format.HumanSize( Convert.ToDecimal( size ), places, lower );
}
public static string HumanSize( double size, int places = 2, bool lower = false )
{
return Format.HumanSize( Convert.ToDecimal( size ), places, lower );
}
public static string HumanSize( decimal size, int places = 2, bool lower = false )
{
string[] units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
int index = 0;
decimal newSize = size;
while( newSize >= 1024.0m )
{
newSize /= 1024.0m;
++index;
}
string unit = lower ? units[index].ToLower() : units[index];
newSize = Decimal.Round( newSize, places, MidpointRounding.AwayFromZero );
return String.Format( "{0:0." + new String( '0', 2 ) + "}{1}", newSize, unit );
}
}
module HumanSize
def human_size( places = 2, lower = false )
units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ]
i = 0
newSize = self + 0.0
until i >= units.length - 2 or newSize < 1024.0
newSize /= 1024.0
i += 1
end
fmt = "%.0" + places.to_s + "f%s"
unit = lower ? units[i].downcase : units[i]
sprintf fmt, newSize, unit
end
end
class Fixnum
include HumanSize
end
class Bignum
include HumanSize
end
class Integer
include HumanSize
end
class Float
include HumanSize
end
class Rational
include HumanSize
end
#Prototype version
Number::humanSize = ( places = 2, lower = false ) ->
units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ]
newSize = @ * 1.0
index = 0
while newSize > 1024.0
newSize /= 1024.0
++index
unit = if lower then units[index].toLowerCase() else units[index]
return "#{+newSize.toFixed( places ).toString()}#{unit}"
#Non-prototype
humanSize = ( size, places = 2, lower = false ) ->
units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ]
newSize = size * 1.0
index = 0
while newSize > 1024.0
newSize /= 1024.0
++index
unit = if lower then units[index].toLowerCase() else units[index]
return "#{+newSize.toFixed( places )}#{unit}"
module humanSize;
private import std.traits : isNumeric;
private import std.string : toLower, format;
string humanSize( T )( T size, bool lower = false ) if( isNumeric!T )
{
string[] units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ];
real newSize = cast( real )size;
int index = 0;
while( newSize >= 1024.0 )
{
newSize /= 1024.0;
++index;
}
string unit = lower ? units[index].toLower : units[index];
return "%0.2f%s".format( newSize, unit );
}
//Prototype version
Number.prototype.humanSize = function( places, lower ) {
if( places == null )
places = 2;
if( lower == null )
lower = false;
var units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ];
var newSize = this * 1.0;
var index = 0;
while( newSize >= 1024.0 )
{
newSize /= 1024.0;
++index;
}
var unit = lower ? units[index].toLowerCase() : units[index];
return ( +newSize.toFixed( places ) ).toString() + unit;
};
//Non-prototype
function humanSize( size, places, lower )
{
if( places == null )
places = 2;
if( lower == null )
lower = false;
var units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ];
var newSize = size * 1.0;
var index = 0;
while( newSize >= 1024.0 )
{
newSize /= 1024.0;
++index;
}
var unit = lower ? units[index].toLowerCase() : units[index];
return ( +newSize.toFixed( places ) ).toString() + unit;
}
<?php
//For PHP ≥ 5.4.x
function humanSize( $size, $decimalPlaces = 2, $lower = false )
{
$units = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ];
$index = 0;
$newSize = (double)$size;
while( $newSize >= 1024.0 )
{
$newSize /= 1024.0;
++$index;
}
$unit = $lower ? strtolower( $units[$index] ) : $units[$index];
$newSize = number_format( round( $newSize, $decimalPlaces ), $decimalPlaces );
return sprintf( "%0.2f%s", $newSize, $unit );
}
//For PHP ≥ 5.x
function humanSize( $size, $decimalPlaces = 2, $lower = false )
{
$units = array( "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" );
$index = 0;
$newSize = (double)$size;
while( $newSize >= 1024.0 )
{
$newSize /= 1024.0;
++$index;
}
$unit = $lower ? strtolower( $units[$index] ) : $units[$index];
$newSize = number_format( round( $newSize, $decimalPlaces ), $decimalPlaces );
return sprintf( "%0.2f%s", $newSize, $unit );
}
def humanSize( size, places = 2, lower = False ):
units = ( "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" )
index = 0
newSize = float( size )
while newSize >= 1024.0:
newSize /= 1024.0
index += 1
unit = units[index].lower() if lower is True else units[index]
newSize = round( newSize, places )
return "%0.2f%s" % ( newSize, unit )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment