简体   繁体   中英

C# three part format output issue

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.12 but 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));
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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM