csharp 用于以人类可读方式表示任意字节长度的实用程序。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 用于以人类可读方式表示任意字节长度的实用程序。相关的知识,希望对你有一定的参考价值。
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
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 )
<?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 );
}
//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;
}
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::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}"
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 );
}
}
以上是关于csharp 用于以人类可读方式表示任意字节长度的实用程序。的主要内容,如果未能解决你的问题,请参考以下文章