The title pretty much says it all. I feel like I've come across a method in the past that does something like:

long fileSize = 524288000;
string prettyFileSize = Sitecore.SomeUtility.ConvertPrettyFileSize(fileSize);

Resulting in prettyFileSize containing "512MB".


You are correct, you probably have come across this. The Sitecore.StringUtil, Sitecore.Kernel class contains a number of methods for managing strings.

The two methods in particular are:

StringUtil.GetSizeString(long size)

This method takes in a long value 524288000, and returns a string representation of "512MB"

StringUtil.ParseSizeString(string value)

This method takes in a string value like "512MB" and returns the long representation of that size, 524288000.

An Example:

    using Sitecore;

    public long GetLongValue(string valueSize)
       return StringUtil.ParseSizeString(valueSize);

    public string GetStringSize(long size)
       return StringUtil.GetSizeString(size);
| improve this answer | |
  • Just a note, as I needed to answer this for somebody else, but the first method here (GetStringSize) was marked as obsolete in version 9.2. You now need to use Sitecore.MainUtil.FormatSize(long, bool) in the Kernel DLL as of 9.3. – Jason St-Cyr Jun 3 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.