简体   繁体   English

C# 三部分格式 output 问题

[英]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/最近在这篇文章中读到关于C#三部分显示格式: https://diptimayapatra.wordpress.com/2014/01-number-cin-格式/

The 3 part format is used to show a positive, negative and zero value number. 3 部分格式用于显示正数、负数和零值数。 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"结果将是“(+)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" .但结果是 "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 .基于上述逻辑,我希望结果应该是29.12但最终变成2929.12 Why using .为什么使用. inside will become this unpredictable output?里面会不会变成这个不可预知的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: output 在这种情况下是相同的:

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.这意味着, 291是 output 不变。 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:如果您想从字面上理解 output ,则需要对其进行转义或将其放在引号中:

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 ?如果实际数量为76.88 ,谁愿意获得 output 29.12

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 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 https://dotnetfiddle.net/jURVtm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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