개발/C#

C# 파일사이즈 Long -> String 변환

혈중마라농도 2022. 1. 6. 16:47
public static string GetFileSize(long size)
{
    if(size < 0)
    {
        return "Invalid size";
    }
    else if(size == 0)
    {
        return "0 Byte";
    }
    else if(size < 1024)
    {
        return string.Format("{0:0.} Bytes", size);
    }
    else if (size < 1024 * 1024)
    {
        return string.Format("{0:0.} KB", size / 1024);
    }
    else if (size < 1024 * 1024 * 1024)
    {
        return string.Format("{0:0.} MB", size / 1024 / 1024);
    }
    else
    {
        return string.Format("{0:0.} GB", Math.Truncate(size / (Double)(1024 * 1024 * 1024) * 10) / 10);
    }
}
반응형