C#中B、KB、MB、GB、TB转换
效果图:代码:
/// <summary>
/// 转换事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton2_Click(object sender, EventArgs e)
{
this.textEdit2.Text = HumanReadableFilesize(Convert.ToDouble(textEdit1.Text));
}
/// <summary>
/// 转换方法
/// </summary>
/// <param name="size">字节值</param>
/// <returns></returns>
private String HumanReadableFilesize(double size)
{
String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
double mod = 1024.0;
int i = 0;
while (size >= mod)
{
size /= mod;
i++;
}
return Math.Round(size) + units;
}
叶子
页:
[1]