CSharp · Console
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(FormatSpeed(100.0, 10.0));
Console.WriteLine(FormatSpeed(1000.0, 10.0));
Console.WriteLine(FormatSpeed(2000.0, 10.0));
Console.WriteLine(FormatSpeed(1024000.0, 10.0));
Console.WriteLine(FormatSpeed(1024000000.0, 10.0));
Console.WriteLine(FormatSpeed(100 * 1024 * 1024.0, 10.0));
}
static string FormatSpeed( double size, double tspan )
{
string message = "Download Speed: ";
if( size/tspan > 1024 * 1024 ) // MB
{
return string.Format(message + "{0:N1} {1}", size/(1024*1204)/tspan, "MB/s");
}else if( size/tspan > 1024 ) // KB
{
return string.Format(message + "{0:N0} {1}", size/1024/tspan, "KB/s");
}
else
{
return string.Format(message + "{0:N1} {1}", size/1024/tspan, "KB/s");
}
}
}