Recent

Author Topic: [Solved] - Formatting a file size to something more human readable  (Read 2936 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] - Formatting a file size to something more human readable
« Reply #15 on: October 17, 2023, 10:29:10 pm »
My way:
Code: Pascal  [Select][+][-]
  1.   function SizeToString(const ASize: UInt64; const oneKB: Integer = {$IFDEF MSWINDOWS}1024{$ELSE}1000{$ENDIF}): string;
  2.   var
  3.     Calc: Extended;
  4.     Sign: String;
  5.     SizeLength: Integer;
  6.   begin
  7.     SizeLength := 0;
  8.     Calc := ASize;
  9.     while (Calc > oneKB) do
  10.       begin
  11.         Calc := Calc / oneKB;
  12.         Inc(SizeLength);
  13.       end;
  14.     case SizeLength of
  15.       0: Sign := ' byte';
  16.       1: Sign := ' kb'; // Kilo Byte
  17.       2: Sign := ' mb'; // Mega Byte
  18.       3: Sign := ' gb'; // Giga Byte
  19.       4: Sign := ' tb'; // Tera Byte
  20.       5: Sign := ' pb'; // Peta Byte
  21.       6: Sign := ' eb'; // Exa Byte
  22.       7: Sign := ' zb'; // Zetta Byte
  23.       8: Sign := ' yb'; // Yotta Byte
  24.       9: Sign := ' rb'; // Ronna Byte
  25.       10:Sign := ' qb'; // Quetta Byte
  26.     else
  27.       Sign :=' (' + IntToStr(SizeLength) + ')';
  28.     end;
  29.     Result := FormatFloat('#,##0.00', Calc) + Sign;
  30.   end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: [Solved] - Formatting a file size to something more human readable
« Reply #16 on: October 18, 2023, 10:19:04 am »
I love how this has sparked a discussion here.  THe function I quoted before does the job for me for sure, but I'm learning a lot from the other postings here.

Thanks Guys,

CJ

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: [Solved] - Formatting a file size to something more human readable
« Reply #17 on: October 18, 2023, 01:02:03 pm »
@zxandris
Did you try your initial function with the value 1048576+1048575?
My posts on the topic were to point you at your prime mistake there:
Code: Pascal  [Select][+][-]
  1.               ...
  2.               md := size mod GB;
  3.               mdStr := IntToStr(md);
  4.               if length(mdStr)>2 then mdStr := leftStr(mdStr, 2);
  5.               rtn := CommaTHou(sze) + '.' + mdStr + ...
  6.               ...
I suppose it was a trick you have used earlier, but it will work only when the base of the displayed (10^x) is the same, which in this particular case is not (2^x).
« Last Edit: October 18, 2023, 01:09:53 pm by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: [Solved] - Formatting a file size to something more human readable
« Reply #18 on: October 18, 2023, 02:38:13 pm »
@zxandris
Did you try your initial function with the value 1048576+1048575?
My posts on the topic were to point you at your prime mistake there:
Code: Pascal  [Select][+][-]
  1.               ...
  2.               md := size mod GB;
  3.               mdStr := IntToStr(md);
  4.               if length(mdStr)>2 then mdStr := leftStr(mdStr, 2);
  5.               rtn := CommaTHou(sze) + '.' + mdStr + ...
  6.               ...
I suppose it was a trick you have used earlier, but it will work only when the base of the displayed (10^x) is the same, which in this particular case is not (2^x).

I hate to have to admit this, but you may as well be talking latin here, actually I understand latin a little better than I do math :).  I strongly suspect that my original function was wrong, it's why I posted here in the first place.  I make do by using google most of the time but the bases involved here fox me.

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: [Solved] - Formatting a file size to something more human readable
« Reply #19 on: October 19, 2023, 08:34:54 am »
@zxandris
I don't know how it is possible to google that kind of wrong result, but see what happens with some example args:
Code: Pascal  [Select][+][-]
  1.   WriteLn(strFormatSize(1024+1));      // output: 1.1 kb
  2.   WriteLn(strFormatSize(1024+102));    // output: 1.10 kb
  3.   WriteLn(strFormatSize(1024+1023));   // output: 1.10 kb
  4.   WriteLn(strFormatSize(1024+9));      // output: 1.9 kb
  5.   WriteLn(strFormatSize(1024+90));     // output: 1.90 kb
  6.   WriteLn(strFormatSize(1024+900));    // output: 1.90 kb

Recently, I have noticed one of the forum members used in its own signature the quote:
Quote
“Good judgement is the result of experience and experience the result of bad judgement.”
― Mark Twain
So, it is quite important to understand your own mistakes in order to make good judgements later. Otherwise you'll continue to use some half-witted (googled or AI generated) code in your programs.

The Kays post has some useful info about the topic.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018