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 : 27945
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
- VB 2005, VB 2008, VB.NET 1.x
- VB 2008
- VB6, VBA



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);

Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 26/11/2008
Posté par PCPT [Liste]
    Public Function GetSizeName(ByVal lValue As LongAs String
        If (lValue = 0Or (lValue = 1Then
            Return lValue.ToString & " octet"
        ElseIf lValue < 0 Then
            Return String.Empty
        End If
        Dim asLibs() As String = {"octets""Ko""Mo""Go""To"}
        Dim r As Double = Convert.ToDouble(lValue)
        Dim d As Integer = asLibs.GetLowerBound(0)
        Dim s As Boolean
        Do While r > 1024
            s = Not ((Convert.ToInt64(r) And 1023= 0)
            r = r / 1024
            d += 1
        Loop
        Dim sRet As String = String.Format("{0} {1}", Math.Round(r, 3), asLibs(d))
        asLibs = Nothing
        Return sRet
    End Function
Remarque :
TextBox2.Text = GetSizeName(Int64.Parse(TextBox1.Text))
Langage : VB 2008
Date ajout : 12/01/2009
Posté par elguevel [Liste]

    ''' <summary>
    ''' Converti une taille exprimée en octet en une chaine formattée
    ''' </summary>
    ''' <param name="Entier">Taille en octet</param>
    ''' <returns>Chaine formattée avec l'unité</returns>    
    Public Function ConvertSizeFile(ByVal Entier As Long) As String

        If (Entier >= &H10000000000) Then
            Return String.Format("{0:N2} To", Entier / &H10000000000)
        ElseIf (Entier >= &H40000000) Then
            Return String.Format("{0:N2} Go", Entier / &H40000000)
        ElseIf (Entier >= &H100000) Then
            Return String.Format("{0:N2} Mo", Entier / &H100000)
        ElseIf (Entier >= &H400) Then
            Return String.Format("{0:N2} Ko", Entier / &H400)
        Else
            Return String.Format("{0} oc", Entier)
        End If

    End Function

Langage : VB6 , VBA
Date ajout : 11/03/2010
Posté par Renfield [Liste]
Private Declare Function StrFormatByteSize Lib "shlwapi.dll" Alias "StrFormatByteSizeA" (ByVal dw As Long, ByVal szBuf As String, ByVal uiBufSize As Long) As Long
Public Function FormatSize(ByVal vnSize As Long) As String
Dim sBuffer As String * 128
Dim nPos As Long
   If StrFormatByteSize(vnSize, sBuffer, 128) Then
       nPos = InStr(sBuffer, vbNullChar)
       If nPos Then
           FormatSize = Strings.Left$(sBuffer, nPos - 1)
       End If
   End If
End Function