4/9/12

Làm thế nào đưa định dạng Byte sang định dạng thích hợp(Bytes. KB, MB, GB)

Category: Date andMath
Difficuty: Beginning
Author: Quang.IT

Hàm sử dụng để Convert từ Byte sang định dạng thích hợp

Ví dụ: SetBytes(74) 'returns "74 Bytes"
        SetBytes(5037) 'returns "4.92 KB"
        SetBytes(6383838) 'returns "6.09 MB"
        SetBytes(3368383278) 'returns "3.14 GB"

private string GetFileSize(double byteCount)
        {
            string size = "0 Bytes";
            if (byteCount >= 1073741824.0)
                size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
            else if (byteCount >= 1048576.0)
                size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB";
            else if (byteCount >= 1024.0)
                size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB";
            else if (byteCount > 0 && byteCount < 1024.0)
                size = byteCount.ToString() + " Bytes";

            return size;
        }

Còn đây là VB:
Function SetBytes(Bytes) As String

On Error GoTo hell

If Bytes >= 1073741824 Then
    SetBytes = Format(Bytes / 1024 / 1024 / 1024, "#0.00") _
         & " GB"
ElseIf Bytes >= 1048576 Then
    SetBytes = Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
    SetBytes = Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes < 1024 Then
    SetBytes = Fix(Bytes) & " Bytes"
End If

Exit Function
hell:
SetBytes = "0 Bytes"
End Function
Bookmark and Share

0 comments:

Post a Comment

Next previous home

Cộng đồng yêu thiết kế Việt Nam Thiet ke website, danang