如果汇总则嵌套 [英] nested if summarization

查看:51
本文介绍了如果汇总则嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Friends
我在4行中总结此语句时遇到问题.
有人可以帮我吗?


Hi Friends
I have a problem with summarizing this statement in 4 lines.
could anyone help me please?


private static string GetSize(long length)
        {
            if (length >= 1024 * 1024 * 1024)
                return string.Format("{0:f2} GB", (double)length / (1024.0 * 1024.0 * 1024.0));
            else if (length >= 1024 * 1024)
                return string.Format("{0:f2} MB", (double)length / (1024.0 * 1024.0));
            else if (length >= 1024)
                return string.Format("{0:f2} KB", (double)length / (1024.0));

            return string.Format("{0} Bytes", length);
        }




在预标签中包装代码[/编辑]




Wraped code in pre tags[/Edited]

推荐答案

我不知道通过概括一下这是什么意思,您可以通过使用减少行数这 "?"操作员,但以可读性/可维护性为代价.例如:
I''m not sure what you mean by summarizing this, you could reduce the lines count by using the "?" operator, but at the expense of readablility / maintainability. For example:
return length >= 1024 * 1024 * 1024 ? string.Format("{0:f2} GB", (double)length / (1024.0 * 1024.0 * 1024.0)) : length >= 1024 * 1024 ? ...



就我个人而言,我会把它留给别人听.



Personally, I would leave it pretty much alone.


这篇关于如果汇总则嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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