Created
October 27, 2017 08:03
-
-
Save ps-team/9fc9675179da2e970b71371f7d1aed97 to your computer and use it in GitHub Desktop.
Output file size for related documents and convert to KB, MB, GB etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Something like this | |
@{ | |
var RelatedDocuments = CurrentNode.RelatedNodes("Related Documents"); | |
} | |
@foreach(var item in RelatedDocuments) { | |
var itemSize = SizeSuffix(item.Size); | |
@itemSize | |
} | |
@functions { | |
static readonly string[] SizeSuffixes = | |
{ "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; | |
static string SizeSuffix(Int64 value) | |
{ | |
if (value < 0) { return "-" + SizeSuffix(-value); } | |
if (value == 0) { return "0.0 bytes"; } | |
int mag = (int)Math.Log(value, 1024); | |
decimal adjustedSize = (decimal)value / (1L << (mag * 10)); | |
return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment