5

This is a little hard to read:

int x = 100000000;

Is there an easy way to write:

int x = 100,000,000;

So that the scale is obvious?

Please note this is NOT about formatting the output.

coder
  • 5,200
  • 3
  • 20
  • 45
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

6 Answers6

11

Starting with C#7 you will be able to use the underscore-char (_) to seperate digit groups. The underscore can be placed anywhere in a number. A simple example:

const int MILLION = 1_000_000;

You can place the _ anywhere you want and can even combine it with integer literals:

const int USHORT_MAX = 0xFF_FF;

Or even with floats and decimals, even after the decimal sign:

const decimal SMALL_VALUE = 0.000_000_001;
Jeankowkow
  • 814
  • 13
  • 33
crazy_crank
  • 659
  • 4
  • 17
  • Damn, that feature slipped. Last year it was supposed to be a part of C# 6. See at the end of https://msdn.microsoft.com/en-us/magazine/dn683793.aspx – Peter M Jun 05 '15 at 17:49
  • darn... they cut a few nice features... I weep for the auto-property initializers – crazy_crank Jun 05 '15 at 17:51
  • @crazy_crank, Auto-Property initializers are there in C# 6.0 (https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14) right ? – Habib Jun 05 '15 at 19:40
  • ok, either they put that in again, or I've completely missinterpreted something here... – crazy_crank Jun 08 '15 at 21:14
  • @crazy_crank - C#7 will include digit separators. It is shipped with the VS 15 Preview. – Ralf Bönning Aug 16 '16 at 21:21
6

Is there any easy way to write 100,000,000

No. It is not possible to put commas in numeric.

Comma and other similar things are for presentation purpose. If you are worried about code readability then either declare constants with name like MILLION, TEN_MILLIONS etc or simply name the variable to represent value.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

If the purpose is to help the reader understand the code, then code comments may be appropriate, e.g.

int number = 1000000; //1,000,000

If you think scientific notation is easier to read, you could try

int number = 1 * 10^6;

or

int number = (int) 1E+6;

Constants like this are computed at compile time and do not affect performance.

John Wu
  • 50,556
  • 8
  • 44
  • 80
1

Unfortunately you can't have commas in your int declarations. Probably the best thing to add something like this at the beginning of your code:

const int oneMillion = 1000000;

Then just set int x = oneMillion;

If you'd like int y to equal 2 million, then just set int y = 2 * oneMillion;

That's best practice (in my experience).

Does that help?


EDIT:

For more elaborate integers, you can try something like this:

const int thousand = 1000;
const int million = 1000000;

int x = 1 * million
           + 233 * thousand
           + 456;

It's not the prettiest, but perhaps it suits your readability needs. It also doesn't affect performance nearly as much as Int32.Parse("100,000,000");

Ben Holland
  • 321
  • 1
  • 8
1
int x = Int32.Parse("100,000,000", System.Globalization.NumberStyles.Number);

Whether or not that's more readable is a personal preference, I suppose.

Chris Steele
  • 1,343
  • 1
  • 9
  • 20
  • I think this answers the posted question directly and to the point. – dub stylee Jun 05 '15 at 17:17
  • 8
    It's a pretty silly thing to do, though. If performance isn't an issue at all then sure, this approach would "work" (for certain values of "work"). But it also moves a fairly significant potential for errors from compile-time to run-time. Which is kind of a bad thing. It solves the "problem", I'll give you that. But I think the OP is just trading a small problem today for a large problem tomorrow. – David Jun 05 '15 at 17:20
0

You could create your own class for your number, something like this.

public class MyNumber
{
    long? _myValue = null;

    public MyNumber(string amount)
    {
        long result = 0;
        if (long.TryParse(amount.Replace(",", ""), out result))
        {
            _myValue = result;
        }
        else
            _myValue = 0;
    }

    public bool HasValue()
    {
        if (_myValue.HasValue) return true;
        return false;
    }

    public long Value()
    {
        if (this.HasValue())
        {
            return _myValue.Value;
        }
        else
        {
            return 0;
        }
    }
}

Then you could call it like this.

        MyNumber myValue = new MyNumber("9,999,100");

        if (myValue.HasValue())
            MessageBox.Show("Value Is " + myValue.Value().ToString());
Rob Stewart
  • 441
  • 4
  • 14
  • This is a far worse form of the answer that suggests using `Int32.Parse`. – Preston Guillot Jun 05 '15 at 19:29
  • I was just answering the question, not suggesting that any way is better than the other. And for your information the Int32.Parse statement above generates a format exception because it can't handle commas. – Rob Stewart Jun 05 '15 at 21:20
  • Yep, you are right, I forgot the NumberStyles.Number in there to get it to parse punctuation. I'll fix it. – Chris Steele Jun 05 '15 at 21:56