Using underscore as a digit separator

Underscores can be used a digit separator on numeric literals for ease of reading

Home DailyDrop

Daily Knowledge Drop

An underscore (_) can be used to separate digits when dealing with numeric literals to make it easier to read.


Decimal literals

When dealing with large numeric literals, it can sometimes be difficult to read them.

Consider a value representing pi to 100 digits:

    var pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;

This can instead be represented as follows, with a 3 digit separator. This has no effect on the actual value, just how it appears:

    var pi3 = 3.141_592_653_589_793_238_462_643_383_279_502_884_197_169_399_375_105_820_974_944_592_307_816_406_286_208_998_628_034_825_342_117_067_9;

The same formatting can also be applied as a thousand separator, to make balances easier to read, for example:

    var balance = 75098217932.65;

This instead becomes:

    var balanceSeparated = 75_098_217_932.63;

Adding the separator has no effect on the underlying values, just increases the readability of the values.


Hexadecimal and binary literals

The formatting can also be applied to hexadecimal and binary literals:

A hexadecimal literal:

    var hexValue = 0x2DFDBBC31;
    var hexValueSep = 0x_2_DF_DB_BC_31;

The 0x prefix indicates a hexadecimal literal, and as you can see the separator can be applied.

The same can be applied to a binary literal:

    var byteValue = 0b10011010010;
    var byteValueSep = 0b_100_1101_0010;

The 0b prefix indicates a binary literal.


Notes

A small feature learnt today, to assist in making code slightly more readable.


References

Integral numeric types (C# reference)
What's New in C# 7.0?

Daily Drop 40: 29-03-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net separator numeric