Search code examples
c#arraysmultidimensional-arrayplaceholder

What is the purpose of -5 in placeholder? c#


We've been creating a program where it has to show the table or array. I just want to know the purpose of -5 in the placeholder Console.Write("{0, -5}", Greater[row, col]);


Solution

  • -5 is a formatting specifier to determine the width of the output string for this placeholder

    5 means the string will be at least 5 chars wide (padded with spaces) and the - means the value produced by accessing the Greater array will be aligned to the left

    If the value in Greater at that row,col is 1.2 the output will be "1.2 "

    If the value were 1.23456789 the output would not be cut off at 5 wide. To ensure the data is formatted to a certain presentation you can add another formatting specifier after a colon, to dictate how the number will be formatted. For more info see "Control Spacing" onwards in https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0