0

Recently, I read about C# three part display format in this article: https://diptimayapatra.wordpress.com/2014/01/13/3-part-format-of-numbers-in-c/

The 3 part format is used to show a positive, negative and zero value number. The string format has three parts. The first part corresponds to the positive test and the second part corresponds to negative test. The last part is for when the value is zero.

The result of this will be "(+) 29.12"

  double value1 = 29.12;
  string formattingString = $"(+) #.##; (-) #.##; No value provided";
  Console.WriteLine(value1.ToString(formattingString));

The result of this will be "positive"

  double value1 = 29.12;
  string formattingString = $"positve; negative; No value provided";
  Console.WriteLine(value1.ToString(formattingString));

But the result of this is "2929.12". Why is this so?

Based on above logic, i expect that the result should be 29.12but it ends up becoming 2929.12. Why using . inside will become this unpredictable output?

  double value1 = 29.12;
  string formattingString = $"29.12; Negative; No value provided";
 Console.WriteLine(value1.ToString(formattingString));
4
  • 1
    those aren't valid format strings: learn.microsoft.com/en-us/dotnet/standard/base-types/… -- rather you are mixing standard format strings with custom ones. its a mess... Commented Mar 9, 2021 at 15:24
  • @DanielA.White Why are they invalid? All other characters: The character is copied to the result string unchanged. Commented Mar 9, 2021 at 15:38
  • @ThomasWeller its not valid as it creates confusion Commented Mar 9, 2021 at 15:39
  • I agree, it is confusing, because nobody would use it like that. But valid according the specification - maybe. Commented Mar 9, 2021 at 15:44

2 Answers 2

2
double value1 = 29.12;
string formattingString = $"29.12; Negative; No value provided";
Console.WriteLine(value1.ToString(formattingString));

You provide a positive number as input, so only the first part of the format string is relevant. The output is identical in this case:

formattingString = $"29.12";   // removed irrelevant three part formatting

The documentation says:

All other characters: The character is copied to the result string unchanged.

That means, 2, 9 and 1 are output unchanged. Obviously the . is responsible for outputting the actual number and it is:

double value1 = 29.12;
string formattingString = $"29.12; Negative; No value provided";
Console.WriteLine(value1.ToString(formattingString));
formattingString = $"29.12";
Console.WriteLine(value1.ToString(formattingString));
formattingString = $".";
Console.WriteLine(value1.ToString(formattingString));

The result is more obvious with a different number:

double value1 = 76.88;

The decimal point is a special character:

Decimal point: Determines the location of the decimal separator in the result string.

If you want to output that one literally, you need to escape it or put it in quotes:

formattingString = $"29\\.12";    // Escape character
formattingString = $"\"29.12\"";  // Literal string delimiter 1
formattingString = $"'29.12'";    // Literal string delimiter 2

Please note that the actual use case for such a format string is doubtful. Who would like to get the output 29.12 if the actual number is 76.88?

Sign up to request clarification or add additional context in comments.

1 Comment

@eulercode: Please note that the actual use case for such a format string is doubtful. Who would like to get the output 29.12 if the actual number is 76.88?
1

If you want them to be the literal strings, enclose them in single quotes.

double value1 = 29.12;
string formattingString = $"'positve'; 'negative'; 'No value provided'";
Console.WriteLine(value1.ToString(formattingString));

https://dotnetfiddle.net/KEVh8b

double value1 = 29.12;
string formattingString = $"'29.12'; 'negative'; 'No value provided'";
Console.WriteLine(value1.ToString(formattingString));

https://dotnetfiddle.net/jURVtm

4 Comments

So u mean i need to enclose the 29.12 in single quotes or else it wouldnt be treat as string?
@eulercode right - in those single quotes its verbatim, not formatting instructions
Yes, i get u now but would you mind to explain how without enclosing it it will produce 2929.12 instead of 29.12? would like to know how it works underlying
@eulercode look at the formatting string documentation that should explain it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.