private void TestFormatting(long value, string expected) { var unitAndScale = UnitAndScale.GetPreferredByteFormat(value); string actual = unitAndScale.GetFormatted(value) + " " + unitAndScale.Unit; Assert.AreEqual(expected, actual); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var progressWithSpeed = value as DirectoryPatchPhaseProgressWithSpeed; if (progressWithSpeed == null) { return("unknown"); } var progress = progressWithSpeed.ProgressReport; if (progress == null) { return("unknown"); } if (progress.State == DirectoryPatchPhaseProgress.States.Unstarted) { return("not started"); } else if (progress.State == DirectoryPatchPhaseProgress.States.Finished) { return("finished"); } else if (progress.Size.Total == 0) { return("pending"); } else if (progress.State == DirectoryPatchPhaseProgress.States.Indeterminate) { var unitAndScale = UnitAndScale.GetPreferredByteFormat(progress.Size.Total); var speedUnitAndScale = UnitAndScale.GetPreferredByteFormat(progressWithSpeed.BytesPerSecond); double perc = (progress.Size.Done / (double)progress.Size.Total) * 100.00; return(string.Format("{0} / ~{1} {2} ({3} {4}/s - {5:#.##}%)", unitAndScale.GetFormatted(progress.Size.Done), unitAndScale.GetFormatted(progress.Size.Total), unitAndScale.Unit, speedUnitAndScale.GetFormatted(progressWithSpeed.BytesPerSecond), speedUnitAndScale.Unit, perc)); } else { var unitAndScale = UnitAndScale.GetPreferredByteFormat(progress.Size.Total); var speedUnitAndScale = UnitAndScale.GetPreferredByteFormat(progressWithSpeed.BytesPerSecond); double perc = (progress.Size.Done / (double)progress.Size.Total) * 100.00; return(string.Format("{0} / {1} {2} ({3} {4}/s - {5:#.##}%)", unitAndScale.GetFormatted(progress.Size.Done), unitAndScale.GetFormatted(progress.Size.Total), unitAndScale.Unit, speedUnitAndScale.GetFormatted(progressWithSpeed.BytesPerSecond), speedUnitAndScale.Unit, perc)); } }