/// <summary> /// Convert total amount of bytes to a human-readable string. /// The output format is controlled through properties of the input /// <see cref="TrafficUnitValue"/>. Scale is decided basing /// on the size amount, however unit type (bits or Bytes) is taken /// directly from the input param. /// </summary> /// <returns> /// Human-readable traffic scale in example format "12.34 Mb". /// </returns> public static string Humanize(TrafficUnitValue trafficValue) { double total = trafficValue.Value; string transferUnit = trafficValue.InBits ? "b" : "B"; string sizeFactorName = ""; float sizeFactor = 1.0f; if (total >= Gi) { sizeFactor = Gi; sizeFactorName = "G"; } else if (total >= Mi) { sizeFactor = Mi; sizeFactorName = "M"; } else if (total >= Ki) { sizeFactor = Ki; sizeFactorName = "K"; } return(String.Format("{0:0.0} {1}{2}", total / sizeFactor, sizeFactorName, transferUnit)); }
/// <summary> /// Gets the "total amount" text for a given statistic type. /// The unit type of the text depends on the unit type used for graph painting. /// </summary> private string TotalText(Stat stat) { float[] totals = TrafficStat.Total(stat, TotalsAverageCount); if (totals.Length > 0) { bool bits = GraphPaint.Scale.InBits; // TODO bad programming practice, code smell. float average = totals.Average(); if (bits) { average *= 8.0f; } var trafficValue = new TrafficUnitValue(average, bits); return(TrafficUnit.Humanize(trafficValue) + "/s"); } return("N/A"); }