[英]long integer literals
I had to deal with the code that does calculation with big number eg 我不得不处理用大数字计算的代码,例如
long foo = 6235449243234;
This is hard to tell what is the order of magnitude. 这很难说是什么数量级。 I'd like to write it
我想写一下
long foo = 6_235_449_243_234;
Or 要么
long foo = @6 235 449 243 234;
But C# doesn't have these features. 但是C#没有这些功能。 How to make number literals more readable?
如何使数字文字更具可读性?
Comment it 评论它
long foo = 6235449243234; // 6 23...
Convert it from string 从字符串转换它
long foo = LiteralConverter.toLong(@"6_235_449_243_234");
int mask = LiteralConverter.toInt("b0111_0000_0100_0000");
Any other options? 还有其他选择吗?
Define named constants for these literals, and use comments to explain what the number represents. 为这些文字定义命名常量,并使用注释来解释数字代表的内容。
class MyClass {
///
/// This constant represents cost of a breakfast in Zimbabwe:
/// 6,235,449,243,234
///
const long AvgBreakfastPriceZimbabweanDollars = 6235449243234;
}
Comments every time IMO. 每次IMO评论。 Otherwise, you're just making the code bloated and less than optimal:
否则,你只是让代码臃肿而不是最佳:
long foo = 6235449243234; // 6,235,449,243,234
You could write 你可以写
long lNumber = (long)(6e12 + 235e9 + 449e6 + 243e3 + 234);
But that is not really readable either. 但这也不是真的可读。
For numbers in variables when you are debugging you could write a debugger visualizer . 对于调试时变量中的数字,可以编写调试器可视化工具 。
注释 - 如果可能 - 使用const
或static readonly
值,以便您只在一个地方声明/注释该数字。
Another (unrecommended) way of doing it: 另一种(不推荐的)做法:
static long Parse(params int[] parts)
{
long num = 0;
foreach (int part in parts)
num = num * 1000 + part;
return num;
}
long foo = Parse(6,235,449,243,234);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.