Les Snippets

Connexion

Convertir un nombre en taille (Ko,Mo,Go)

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 25/03/2006 23:35:22 et initié par EBArtSoft [Liste]
Date de mise à jour : 05/08/2006 14:25:59
Vue : 13034
Catégorie(s) : Fichier / Disque, Trucs & Astuces, Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- Delphi 5
- PHP 3, PHP 4, PHP 5
- PHP 3, PHP 4, PHP 5
- Perl



Langage : VB6 , VBA
Date ajout : 25/03/2006
Posté par EBArtSoft [Liste]
Public Function GetSizeName(ByVal vValue As Long) As String
    Dim Desc As Variant
    Dim s    As Boolean
    Dim d    As Integer
    Dim r    As Double
    r = vValue
    Desc = Array("octets", "Ko", "Mo", "Go", "To")
    Do While r > 1024
        s = (r And 1023) <> 0
        r = r / 1024
        d = d + 1
    Loop
    If s Then
        If r > 100 Then
            GetSizeName = Format(r, "0") & " " & Desc(d)
        ElseIf r > 10 Then
            GetSizeName = Format(r, "0.0") & " " & Desc(d)
        Else
            GetSizeName = Format(r, "0.00") & " " & Desc(d)
        End If
    Else
        GetSizeName = r & " " & Desc(d)
    End If
End Function

Langage : Delphi 5
Date ajout : 05/08/2006
Posté par f0xi [Liste]
DateMAJ : 05/08/2006
function SizeToStr(const Size : int64) : string;
begin
  if Size < $000000000400 then
     result := format('%d bytes',[Size]) 
  else
  if Size < $000000100000 then
     result := format('%.1f Kb',[Size/$000000000400]) 
  else
  if Size < $000040000000 then
     result := format('%.1f Mb',[Size/$000000100000]) 
  else
  if Size < $010000000000 then
     result := format('%.2f Gb',[Size/$000040000000]) 
  else
     result := format('%.2f Tb',[Size/$010000000000]) 
end;


Remarque :
attention, selon les normes en vigeur pour l'octet :
1Ko = 1000 octets
1Mo = 1000000 octets
1Go = 1000000000 octets

pour les bytes cela ne change pas ... 1024!
Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 26/05/2007
Posté par iow4 [Liste]
function GetSizeName($octet)
{
    // Array contenant les differents unités 
    $unite = array('octet','ko','mo','go');
    
    if ($octet < 1000) // octet
    {
        return $octet.$unite[0];
    }
    else 
    {
        if ($octet < 1000000) // ko
        {
            $ko = round($octet/1024,2);
            return $ko.$unite[1];
        }
        else // Mo ou Go 
        {
            if ($octet < 1000000000) // Mo 
            {
                $mo = round($octet/(1024*1024),2);
                return $mo.$unite[2];
            }
            else // Go 
            {
                $go = round($octet/(1024*1024*1024),2);
                return $go.$unite[3];    
            }
        }
    }
}
Remarque :
Passé en argument de la fonction des octets
Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 04/06/2007
Posté par coucou747 [Liste]
function int2taille($i, $b=1024){
    $o=$i%1024;
    $k=(int)(($i/$b)%$b);
    $m=(int)(($i/$b/$b)%$b);
    $g=(int)(($i/$b/$b/$b));
    return (($g!=0)?($g.' Go '.$m.' Mo '.$k.' Ko '.$o.' o'):($m!=0)?($m.' Mo '.$k.' Ko '.$o.' o'):(($k!=0)?$k.' Ko '.$o.' o':$o.' o'));
}
Langage : Perl
Date ajout : 04/06/2007
Posté par coucou747 [Liste]
sub int2taille
{
    $i=@_[0];
    $b=@_[1];
    $o=$i%1024;
    $k=int(($i/$b)%$b);
    $m=int(($i/$b/$b)%$b);
    $g=int(($i/$b/$b/$b));
    return (($g!=0)?($g.' Go '.$m.' Mo '.$k.' Ko '.$o.' o'):($m!=0)?($m.' Mo '.$k.' Ko '.$o.' o'):(($k!=0)?$k.' Ko '.$o.' o':$o.' o'));
}
print int2taille(4000, 1024);