locked
Question about Numeric Formatting not seen in documentation RRS feed

  • Question

  • Visual Studio 2019 and .NET allow numbers to be declared with underline spacing for readability.  For example,

       double d = 1234.567_891_23

    If output using:

       Console.WriteLine ($"d = {d:N10}");

    It displays as: 1,234.5678912300

    I've looked through the Standard Numeric Format Strings documentation, but I can't find a format that would display the output from WriteLine as: 1,234.567_891_230_0

    Is there a way to do that?

    Wednesday, August 19, 2020 9:46 PM

Answers

  • Hi MesPia,

    Thank you for posting here.

    You can define the following extension method.

        public static class ExtensionMethod
        {
            public static string ToUnderlineSpacingOutputString(this double value)
            {
                string.Format($"d = {value:N10}");
                string[] strArr = value.ToString().Split('.');
                string s = Regex.Replace(strArr[1], ".{3}", "$0_");
                return strArr[0] + "." + s;
            }
        }

    Then you can use it in Console.WriteLine().

                double d = 1234.567_891_23;
                Console.WriteLine(d.ToUnderlineSpacingOutputString());

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MesPia Thursday, August 20, 2020 1:14 PM
    Thursday, August 20, 2020 3:11 AM

All replies

  • Hi MesPia,

    Thank you for posting here.

    You can define the following extension method.

        public static class ExtensionMethod
        {
            public static string ToUnderlineSpacingOutputString(this double value)
            {
                string.Format($"d = {value:N10}");
                string[] strArr = value.ToString().Split('.');
                string s = Regex.Replace(strArr[1], ".{3}", "$0_");
                return strArr[0] + "." + s;
            }
        }

    Then you can use it in Console.WriteLine().

                double d = 1234.567_891_23;
                Console.WriteLine(d.ToUnderlineSpacingOutputString());

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MesPia Thursday, August 20, 2020 1:14 PM
    Thursday, August 20, 2020 3:11 AM
  • Thanks very much!  I was starting to think about using regular expressions to solve this problem.  Your solution is excellent.
    Thursday, August 20, 2020 1:15 PM
  • If you like regular expressions, maybe write one that avoids Split and unneeded trailing ‘_’.


    • Edited by Viorel_MVP Thursday, August 20, 2020 3:20 PM
    Thursday, August 20, 2020 3:04 PM
  • Hi, I found another solution using custom numeric formatting.  Here it is.

    double x = 1234.56789123;
    WriteLine ($"double x = {x:#,#.###_###_###}");

    Ourput is: x = 1,234.567_891_23

    Tuesday, August 25, 2020 3:48 PM
  • Hi, I found another solution using custom numeric formatting.  Here it is.

    double x = 1234.56789123;
    WriteLine ($"double x = {x:#,#.###_###_###}");

    Ourput is: x = 1,234.567_891_23


    Is it excellent in case of x = 1234.56?


    Tuesday, August 25, 2020 3:56 PM
  • Unfortunately, in that case, it leaves a trailing "_", just like using Split would do in the extension method.  I suppose a better extension method would solve the problem.  I don't like the extension method because it's not as clean or concise as an inline numeric format would be.  It would be nice if Microsoft expanded its Custom Format feature in C#, or allowed C# to be extended.  I wonder it this format may have been added to C# 9.0.
    Tuesday, August 25, 2020 4:10 PM
  • or, I guess I meant, added to .NET 5.0
    Tuesday, August 25, 2020 4:12 PM
  • Another way to handle the "hanging" underline.

       double x = 1234.5678

       WriteLine ("double x = " + string.Format("{0:#,#,###_###},x").TrimEnd('_"));

    correctly outputs:

       1,234.567_8

    Monday, August 31, 2020 6:42 PM