Skip to content

Uniform the string format in Converters.ToFileSizeString #393

@heku

Description

@heku

Describe the bug

The string format output by Converters.ToFileSizeString is not uniform, KB, MB, GB, TB, PB are F1 but EB is F0, I think it should be a typo.

        if (size < 1024)
        {
            return size.ToString("F0") + " bytes";
        }
        else if ((size >> 10) < 1024)
        {
            return (size / 1024F).ToString("F1") + " KB";
        }
        else if ((size >> 20) < 1024)
        {
            return ((size >> 10) / 1024F).ToString("F1") + " MB";
        }
        else if ((size >> 30) < 1024)
        {
            return ((size >> 20) / 1024F).ToString("F1") + " GB";
        }
        else if ((size >> 40) < 1024)
        {
            return ((size >> 30) / 1024F).ToString("F1") + " TB";
        }
        else if ((size >> 50) < 1024)
        {
            return ((size >> 40) / 1024F).ToString("F1") + " PB";
        }
        else
        {
            return ((size >> 50) / 1024F).ToString("F0") + " EB";  // Should be F1 ?
        }

Regression

No response

Steps to reproduce

[TestMethod]
    [DataRow(1024L - 1, "1023 bytes")]
    [DataRow(1024L, "1.0 KB")]
    [DataRow(1024L * 1024, "1.0 MB")]
    [DataRow(1024L * 1024 * 1024, "1.0 GB")]
    [DataRow(1024L * 1024 * 1024 * 1024, "1.0 TB")]
    [DataRow(1024L * 1024 * 1024 * 1024 * 1024, "1.0 PB")]
    [DataRow(1024L * 1024 * 1024 * 1024 * 1024 * 1024, "1 EB")] // NOTE !
    public void Test_ToFileSizeString(long size, string expected)
    {
        Assert.AreEqual(expected, Converters.ToFileSizeString(size));
    }

Expected behavior

    [TestMethod]
    [DataRow(1024L - 1, "1023 bytes")]
    [DataRow(1024L, "1.0 KB")]
    [DataRow(1024L * 1024, "1.0 MB")]
    [DataRow(1024L * 1024 * 1024, "1.0 GB")]
    [DataRow(1024L * 1024 * 1024 * 1024, "1.0 TB")]
    [DataRow(1024L * 1024 * 1024 * 1024 * 1024, "1.0 PB")]
    [DataRow(1024L * 1024 * 1024 * 1024 * 1024 * 1024, "1.0 EB")]  // NOTE !
    public void Test_ToFileSizeString(long size, string expected)
    {
        Assert.AreEqual(expected, Converters.ToFileSizeString(size));
    }

Screenshots

No response

IDE and version

VS 2022 Preview

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

N/A

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛An unexpected issue that highlights incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions