Last active
August 29, 2015 14:02
-
-
Save sirtony/76afd2a07f80a5b60d1f to your computer and use it in GitHub Desktop.
Utilities for representing arbitrary byte-lengths in a human-readable way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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