Digit Separators in C#

Digit Separators in C# with examples

In this article, I am going to discuss the Digit Separators in C# with Examples. Please read our previous article where we discussed Pattern Matching in C# with Examples. At the end of this article, you will understand what Digit Separator is in C# and when and how to use Digit Separators with Examples.

What is Digit Separator in C#?

In reality, it’s very difficult to read a very large number. To overcome this problem, C# 7 comes with a new feature called digit separators “_”. Now, it is possible to use one or more Underscore (_) characters as digit separators in C# to represent a very big number. Sometimes, it is required when we are going to represent a very big number. 

Example: Digit Separator in C#

Let’s understand Digit Separators in C# with an example. Please have a look at the below example. As you can see, here we have created two variables. The first variable is holding a long value while the second variable is also holding a long value but that value is separated by underscores. If you look at the value, both are the same. But the readability is better in the case of the second variable which is split by underscores.

class Program
{
    static void Main()
    {
        // Both are equivalent.
        var bigNumber = 123456789012345678;
        var bigNumberSplit = 123_456_789_012_345_678;

        Console.WriteLine("bigNumber : {0}, bigNumberSplit : {1}", bigNumber, bigNumberSplit);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

When we run the application it gives you the following output.

Digit Separators in C# 7

If you observe in the code, then you feel a little difficult to read the first number as it is a very big number. But, you can easily read the second number because of the thousands separator. The separators do not make any difference in the value as you can see in the above output. You can place them wherever you like in the number, and in any quantity. 

Example: Digit Separator with double

It is also not mandatory to use a single underscore as a separator even though you can also use multiple separators. And it is not restricted to using them with integers only; they also work with the other numeric types like double, float as well. So, let us see an example, where we will use more than one underscores as digit separators as well as working with double value. Please have a look at the following example. Modify the main method as shown below.

class Program
{
    static void Main()
    {
        var myData1 = 1__2________3___4____5_____6;
        var myData2 = 1__2___3___4____5_____6.79;
        Console.WriteLine("bigNumber : {0}, bigNumberSplit : {1}", myData1, myData2);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Now when we run the application, it gives us the output as expected as shown in the below image.

Digit Separators in C#

In the next article, I am going to discuss the Tuples in C# with real-time examples. Here, in this article, I try to explain Digit Separators in C# with examples. I hope you understood the use and need of Digit Separators with Examples.

Leave a Reply

Your email address will not be published.