字节大小转换为文本值描述, 仅显示用...

看代码...

 

uses
  System.Math;

//  ***************************************************************************
//  字节大小转换为文本值描述
//  刘志林
//  2017-11-06
//  http://www.cnblogs.com/lzl_17948876/
//  lzl_17948876@hotmail.com
//  ***************************************************************************
type
  TBSSM = (BSSM_AUTO, BSSM_B, BSSM_KB, BSSM_MB, BSSM_GB, BSSM_TB, BSSM_PB);

/// <summary>
///   字节大小转换为文本值描述
/// </summary>
/// <param name="AByteSize">
///   字节大小
/// </param>
/// <param name="AMode">
///   转换模式, 默认为BSSM_AUTO, 转换为最合适的描述
/// </param>
/// <param name="ADigit">
///   保留小数位数, 默认2位
/// </param>
/// <returns>
///   字符串描述
/// </returns>
function ByteSizeToStr(AByteSize: UInt64; AMode: TBSSM = BSSM_AUTO; ADigit: UInt8 = 2): string;
const
  _L: array[BSSM_KB .. BSSM_PB] of UInt64 = (
    $400, {KB}
    $100000, {MB}
    $40000000, {GB}
    $10000000000, {TB}
    $4000000000000 {PB}
  );
  _T: array[BSSM_B .. BSSM_PB] of string = ('B', 'KB', 'MB', 'GB', 'TB', 'PB');
var
  i: TBSSM;
begin
  if AMode = BSSM_AUTO then
  begin
    AMode := BSSM_B;
    for i := Low(_L) to High(_L) do
    begin
      if AByteSize < _L[i] then
        Break;
      AMode := i;
    end;
  end;
  if AMode = BSSM_B then
    Result := IntToStr(AByteSize) + ' B'
  else
    Result := FloatToStr(RoundTo(AByteSize / _L[AMode], -ADigit)) + ' ' + _T[AMode];
end;

 

posted on 2017-11-06 13:48 堕落恶魔 阅读(...) 评论(...) 编辑 收藏

统计