转换字节GB的C#? [英] Converting bytes to GB in C#?

查看:199
本文介绍了转换字节GB的C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重构一些旧代码,并在下面的代码行字节转换为GB来了。

I was refactoring some old code and came across the following line of code to convert bytes to GB.

decimal GB = KB / 1024 / 1024 / 1024;



有没有更好的方式来重构下面的一段代码?

Is there a better way to refactor the following piece of code?

更新

我的意思是说字节千兆字节。我给了错误的信息。

I meant to say bytes to Gigabytes. I gave wrong information.

推荐答案

我在这里开发了这个方法,工作到TB。

I developed this method here, works up to TB.

private static string FormatBytes(long bytes)
{
    string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
    int i;
    double dblSByte = bytes;
    for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024) 
    {
        dblSByte = bytes / 1024.0;
    }

    return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}

这篇关于转换字节GB的C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆