angularsen / UnitsNet

Makes life working with units of measurement just a little bit better.
https://www.nuget.org/packages/UnitsNet/
MIT No Attribution
2.63k stars 383 forks source link

InformationUnit JEDEC memory standards #909

Closed samds closed 3 years ago

samds commented 3 years ago

Is your feature request related to a problem? Please describe. I would like to use the JEDEC memory standards when showing information unit. Currently, my application shows information in binary bytes (KiB, MiB, GiB, ...) but users are not happy with the prefixes (Ki, Mi, Gi, ...) mostly because they don't know what it is and Microsoft don't use them. They want to see KB, MB, GB as defined by JEDEC. By example, given this standard 1KB is equal to 1024 bytes.

Describe the solution you'd like We could use a specific format in the ToString method.

Describe alternatives you've considered I guess it's possible to change the real number of bytes in order to use the types InformationUnit.Kilobyte, InformationUnit.Megabyte, ... to achieve what I want. By example, the following method should return "1 KB" for 1024 bytes.

        private string ToJEDECKB(double byteCount)
        {
            double intermediateByteCount = byteCount / 1024 * 1000;
            var bytesInfo = UnitsNet.Information.FromBytes(intermediateByteCount);
            return bytesInfo.ToUnit(UnitsNet.Units.InformationUnit.Kilobyte).ToString();
        }

Thanks, Sam

angularsen commented 3 years ago

Hi, you can choose kB vs kiB this way:

Information.FromBytes(1000).ToUnit(InformationUnit.Kilobyte).ToString().Dump(); // 1 kB
Information.FromBytes(1024).ToUnit(InformationUnit.Kibibyte).ToString().Dump(); // 1 KiB

If you want to "override" 1024 bytes to be output as 1 kB, then you would either have to create your own extension method

public static string ToJedecString(this Information i) { /* manipulate numbers as you proposed */ }

or add a Information.ToJedecString() method for other users of UnitsNet to benefit from.

I am in the camp that I think the standard of kB vs KiB help disambiguate the two and I think JEDEC just cause confusion, but I recognize it is widely used in the industry so if you are interested in doing a pull request then I'm happy to assist. I think a separate method is better, since we already have 6 overloads for ToString().

UnitsNet/CustomCode/Information.extra.cs would be the place to add this, see how other quantities have added extra methods.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.