5

I've done a bit of searching for a solution to this (or a previously asked question on SO), but all that turns up are results for formatting numbers in the output of a program, which is not what I'm looking for. My question is, are there any solutions to formatting large numbers IN code (not the output of a program) to make them easier to read.

For instance

int main()
{
    int LargeNumber = 1000000;
}

This number holds 1 million, but its not so easy to tell right away without moving the cursor over it and counting. Are there any good solutions to this besides using a comment?

int main()
{
    int LargeNumber = 1000000;//1,000,000
}

Thank you.

JosephTLyons
  • 2,075
  • 16
  • 39
  • 2
    What is wrong with `1E6` ? At double precision I am pretty sure it will convert to the correct big integer `1000000`. Of course this has limits for VERY big numbers where a 52 bit mantissa does not suffice. – Floris Aug 17 '16 at 04:17
  • Never really considered scientific notation before... thanks for the tip. I'll have to try that out as well. – JosephTLyons Aug 17 '16 at 04:19

1 Answers1

6

The current standard allows you to insert apostrophes as separators in literals, so your code would look like:

int main()
{
    int LargeNumber = 1'000'000;
}

This was added relatively recently (in C++14), however, so if you're using an older compiler, it may not be supported yet. Depending on the compiler, you may also need to add a flag to ask for conformance with the most recent standard to get the compiler to accept this. Offhand I don't remember the exact compiler versions necessary to support it, but it works with the current versions of the major compilers (e.g., g++, clang, and VC++).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Interesting. I'm currently using the newest version of Xcode, so I'll have to look into this. – JosephTLyons Aug 17 '16 at 03:43
  • 2
    @joe_04_04: XCode uses a special (since we're not allowed to call it "retarded" any more) version of Clang. I'd *expect* that it supports this (try `-std=c++14`) but I haven't XCode recently, so I can't say for sure. – Jerry Coffin Aug 17 '16 at 03:47
  • I apologize, but I'm not quite sure where that bit of code goes (I still consider myself a novice in C++). I did try to use apostrophes and it wasn't accepted, but I also didn't know how to implement that short bit of code. – JosephTLyons Aug 17 '16 at 03:54
  • 1
    @joe_04_04: Perhaps [this answer](http://stackoverflow.com/a/23018783/179910) will help. – Jerry Coffin Aug 17 '16 at 03:59
  • Thanks a ton. This helped. Changing the language dialect to C++14 allowed me to use apostrophes as separators. Thank you. – JosephTLyons Aug 17 '16 at 04:04
  • @JerryCoffin: what's special about Apple's `clang`? Never had a problem with it. – Violet Giraffe Aug 17 '16 at 05:02