简体   繁体   English

String.Empty作为“精度说明符”

[英]String.Empty as a “precision specifier”

Jon Skeet said that "" and String.Empty are equivalent due to string interning. 乔恩·斯凯特(Jon Skeet)说""String.Empty由于字符串实习而等效。 In looking at MSDN for ICustomFormatter there is a line in the Format method 在为ICustomFormatter查看MSDN时, Format方法中有一行

// Handle null or empty format string, string with precision specifier.
string thisFmt = String.Empty;

At first I thought they might be adding an empty string in order to avoid dealing with nulls in later logic. 起初我以为他们可能会添加一个空字符串,以避免在以后的逻辑中处理空值。 But this doesn't explain what they mean by "precision specifier". 但这并不能解释“精确说明符”的含义。

What is a "precision specifier"? 什么是“精确说明符”?

When formatting strings to number types a precision specifier can be supplied in the format string as in the following examples: 将字符串格式化为数字类型时,可以在格式字符串中提供精度说明符,如以下示例所示:

 int quantity = 1500;
 float price = 1.50F;
 float discount = 0.05F;

 Console.WriteLine(quantity.ToString("n0"));        // Outputs "1,500"
 Console.WriteLine(price.ToString("c"));            // Outputs "£1.50"
 Console.WriteLine(discount.ToString("p1"));        // Outputs "5.0 %"

In the case of the fixed point and percentage specifier a number is included. 在定点和百分比指定符的情况下,将包括一个数字。 This is the precision specifier and is used to modify the format. 这是精度说明符,用于修改格式。

From here : 这里

The precision specifier ranges from 0 to 99 and controls the number of significant digits or zeros to the right of a decimal. 精度说明符的范围是0到99,并控制小数点右边的有效数字位数或零。

It probably means that later, this variable will contain a format string, which includes a precision specifier. 这可能意味着以后,该变量将包含格式字符串,其中包括精度说明符。 Ie, if a double value is formatted, then you can specify how many digits to the right of the decimal point will be displayed. 即,如果格式化了一个双精度值,则可以指定将显示小数点右边的几位数。 It is not linked to string internals. 它没有链接到字符串内部。

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

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