Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long integer literals

Tags:

c#

.net

I had to deal with the code that does calculation with big number e.g.

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. 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?

like image 490
Lukasz Madon Avatar asked Feb 04 '12 19:02

Lukasz Madon


1 Answers

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;
}
like image 163
Sergey Kalinichenko Avatar answered Oct 01 '22 06:10

Sergey Kalinichenko