skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Make big numbers readable

C# 7

Code card showing that underscores make a billion easier to read without changing its value; hex and binary examples are included. Output: True, System.UInt32, 165.

The compiler ignores underscores in numeric literals. They work in hex and binary too. How many zeroes? 1_000_000_000 vs 1000000000. It is available since C# 7. The key line is var easyBillion = 1_000_000_000 in numbers.cs. It fits in 12 lines; run it with dotnet run numbers.cs.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
numbers.cs
// how many zeroes is a billion?
var billion = 1000000000;
// oh! nine zeroes.
var easyBillion = 1_000_000_000;
Console.WriteLine(billion == easyBillion);
// works with hex and binary too
var color = 0xFF_EF_44_44;
var flags = 0b1010_0101;
Console.WriteLine(color.GetType());
Console.WriteLine(flags);
Output
$ dotnet run numbers.cs
True
System.UInt32
165