internal static string ToSizeWithSuffix(long value, SuffixStyle style, int decimalPlaces = 1) { int num = 1024; if (style == SuffixStyle.Metric) { num = 1000; } if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException(nameof(decimalPlaces)); } if (value == 0L) { return(string.Format("{0:n" + decimalPlaces.ToString() + "} bytes", (object)0)); } int index = (int)Math.Log((double)value, (double)num); Decimal d = (Decimal)value / (Decimal)(1L << index * 10); if (style == SuffixStyle.Metric) { d = (Decimal)value / (Decimal)Math.Pow((double)num, (double)index); } if (Math.Round(d, decimalPlaces) >= 1000M) { ++index; d /= (Decimal)num; } return(string.Format("{0:n2} {1}", (object)d, (object)Helpers.GetSuffixAtIndex(style, index))); }
private static string GetSuffixAtIndex(SuffixStyle style, int index) { switch (style) { case SuffixStyle.Binary: return(SizeBinarySuffixes[index]); case SuffixStyle.Metric: return(SizeMetricSuffixes[index]); case SuffixStyle.Windows: return(SizeWindowsSuffixes[index]); } return(string.Empty); }
internal static string ToSizeWithSuffix(long value, SuffixStyle style, int decimalPlaces = 1) { var newBase = 1024; if (style == SuffixStyle.Metric) { newBase = 1000; } if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); } if (value == 0) { return(string.Format("{0:n" + decimalPlaces + "} bytes", 0)); } // mag is 0 for bytes, 1 for KB, 2, for MB, etc. int mag = (int)Math.Log(value, newBase); // 1L << (mag * 10) == 2 ^ (10 * mag) // [i.e. the number of bytes in the unit corresponding to mag] decimal adjustedSize = (decimal)value / (1L << (mag * 10)); if (style == SuffixStyle.Metric) { adjustedSize = value / (decimal)(Math.Pow(newBase, mag)); } // make adjustment when the value is large enough that // it would round up to higher magnitude if (Math.Round(adjustedSize, decimalPlaces) >= 1000) { mag += 1; adjustedSize /= newBase; } return(string.Format("{0:n" + decimalPlaces + "} {1}", adjustedSize, GetSuffixAtIndex(style, mag))); }
internal static string ToSizeWithSuffix(long value, SuffixStyle style, string format = "{0:0.0}", int roundTo = 1) { var newBase = 1024; if (style == SuffixStyle.Metric) { newBase = 1000; } if (string.IsNullOrWhiteSpace(format)) { throw new ArgumentOutOfRangeException(nameof(format), "The format can't be null."); } if (value <= 0) { return(string.Format($"{format} bytes", 0)); } //mag is 0 for bytes, 1 for KB, 2, for MB, etc. var mag = (int)Math.Log(value, newBase); //1L << (mag * 10) == 2 ^ (10 * mag) //[i.e. the number of bytes in the unit corresponding to mag] var adjustedSize = (decimal)value / (1L << (mag * 10)); if (style == SuffixStyle.Metric) { adjustedSize = value / (decimal)Math.Pow(newBase, mag); } //Make adjustment when the value is large enough that it would round up to higher magnitude. if (Math.Round(adjustedSize, roundTo) >= 1000) { mag += 1; adjustedSize /= newBase; } return(string.Format($"{format} {{1}}", Math.Round(adjustedSize, roundTo), GetSuffixAtIndex(style, mag))); }
public string DataPerSecondFormatted(SuffixStyle suffixStyle, int decimalPlaces) => Helpers.ToSizeWithSuffix((long)this.BytesPerSecond, suffixStyle, decimalPlaces) + "/sec";
public string GetBytesTransferredFormatted(SuffixStyle suffixStyle, int decimalPlaces) => Helpers.ToSizeWithSuffix(this.BytesTransferred, suffixStyle, decimalPlaces);
public string ToStringFormatted(SuffixStyle style = SuffixStyle.Windows, int decimalPlaces = 1) { return($"Total: {GetTotalTransferedFormatted(style, decimalPlaces)}, BytesTransferred: {GetBytesTransferedFormatted(style, decimalPlaces)}, Percentage: {Percentage}"); }
public string ToStringFormatted(SuffixStyle style = SuffixStyle.Windows, string format = "{0:0.0}") { return($"Total: {GetTotalTransferedFormatted(style, format)}, BytesTransferred: {GetBytesTransferedFormatted(style, format)}, Percentage: {Percentage}"); }
public string GetDataPerSecondFormatted(SuffixStyle suffixStyle, string format) { return($"{Helpers.ToSizeWithSuffix((long)BytesPerSecond, suffixStyle, format)}/sec"); }
public string GetDataPerSecondFormatted(SuffixStyle suffixStyle, int decimalPlaces) { return($"{Helpers.ToSizeWithSuffix((long) BytesPerSecond, suffixStyle, decimalPlaces)}/sec"); }
public string GetBytesTransferedFormatted(SuffixStyle suffixStyle, string format) { return(Helpers.ToSizeWithSuffix(BytesTransferred, suffixStyle, format)); }
public string GetBytesTransferedFormatted(SuffixStyle suffixStyle, int decimalPlaces) { return(Helpers.ToSizeWithSuffix(BytesTransferred, suffixStyle, decimalPlaces)); }
public string GetTotalTransferedFormatted(SuffixStyle suffixStyle, string format) { return(Helpers.ToSizeWithSuffix(Total, suffixStyle, format)); }
void OnGUI() { GUILayout.Space(5); GUILayout.Label("Batch Rename a group of objects.", EditorStyles.centeredGreyMiniLabel); GUILayout.Space(5); newName = EditorGUILayout.TextField("New Name:", newName); GUILayout.Space(5); addSuffix = EditorGUILayout.Toggle("Add Suffix?", addSuffix); if (addSuffix) { suffixStyle = (SuffixStyle)EditorGUILayout.EnumPopup("Suffix Style:", suffixStyle); GUILayout.Space(5); string preview = ""; switch (suffixStyle) { case SuffixStyle.Underscore: preview = "Object_1\nObject_2\n..."; break; case SuffixStyle.Dash: preview = "Object - 1\nObject - 2\n..."; break; case SuffixStyle.Brackets: preview = "Object(1)\nObject(2)\n..."; break; case SuffixStyle.SquareBrackets: preview = "Object[1]\nObject[2]\n..."; break; } GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Preview:\n\n" + preview, EditorStyles.miniLabel); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.Space(5); } // Generate button GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Rename", GUILayout.Width(80))) { if (objectsToRename == null || objectsToRename.Length == 0) { return; } Undo.RecordObjects(objectsToRename, "Rename Objects"); for (int i = 0; i < objectsToRename.Length; i++) { if (objectsToRename[i] != null) { if (addSuffix) { switch (suffixStyle) { case SuffixStyle.Underscore: objectsToRename[i].name = newName + "_" + i; break; case SuffixStyle.Dash: objectsToRename[i].name = newName + " - " + i; break; case SuffixStyle.Brackets: objectsToRename[i].name = newName + "(" + i + ")"; break; case SuffixStyle.SquareBrackets: objectsToRename[i].name = newName + "[" + i + "]"; break; } } else { objectsToRename[i].name = newName; } } } } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); //--------------------------------------------------------------------- scrollPos = EditorGUILayout.BeginScrollView(scrollPos); ScriptableObject target = this; SerializedObject so = new SerializedObject(target); SerializedProperty objectsProperty = so.FindProperty("objectsToRename"); EditorGUILayout.PropertyField(objectsProperty, true); so.ApplyModifiedProperties(); EditorGUILayout.EndScrollView(); }
internal static string ToSizeWithSuffix(long value, SuffixStyle style, int decimalPlaces = 1) { return(ToSizeWithSuffix(value, style, $"{{0:n{decimalPlaces}}}", decimalPlaces)); }