[英]String.Format Explanation
I have little confusion in understanding, how the format is being generated for the following line. 我对理解如何为以下行生成格式感到困惑。
Could any one provide me , how its renders the value in Format, With appropriate commas, being put at the right places in the figure. 任何人都可以提供我,它如何呈现格式中的值,使用适当的逗号,放在图中的正确位置。
writer.Write(string.Format("Your Estimated Loan Paymement+will be {0:$#,##0.00;($#,##0.00);Zero}", + this.calcPayment(this._pv,1,2) ));
Here calcPayment()
is a function returns a numeric value. 这里
calcPayment()
是一个返回数值的函数。 For example if it returns 2000.33, then it is outputed as $2,003.33. 例如,如果它返回2000.33,则它被输出为$ 2,003.33。
I know it is doing the formating, but how? 我知道它正在进行格式化,但是如何?
Thank you. 谢谢。
Breaking down the format string: {0:$#,##0.00;($#,##0.00);Zero}
分解格式字符串:
{0:$#,##0.00;($#,##0.00);Zero}
There are 3 groups: 共有3组:
$#,##0.00
- used when the argument ( this.calcPayment(this._pv,1,2)
in this case) is positive. $#,##0.00
- 当参数(本例中为this.calcPayment(this._pv,1,2)
)为正时使用。 ($#,##0.00)
- used when arg is negative ($#,##0.00)
- 当arg为负数时使用 Zero
- used when arg is zero Zero
- 当arg为零时使用 #
is a digit placeholder and 0
is a zero placeholder (padding). #
是一个数字占位符, 0
是零占位符(填充)。
The commas in the part of the format string such as $#,##0.00
tell it whether to place commas (or, as @svick correctly states, "group separators), if needed. Here's a decent reference that describes the format codes half-way down the page: http://blog.stevex.net/string-formatting-in-csharp/ 格式字符串部分中的逗号(例如
$#,##0.00
告诉它是否放置逗号(或者,如@svick正确陈述,“组分隔符”),如果需要。这是一个体面的参考,描述格式代码的一半在页面上: http : //blog.stevex.net/string-formatting-in-csharp/
What's happening is the Format
method (function) is using the format string as a template, and then adding in your additional data provided. 发生的事情是
Format
方法(函数)使用格式字符串作为模板,然后添加您提供的附加数据。
I'm gonna hazard a guess that it's like this: 我会冒险猜测它是这样的:
0: //this is telling it that this is the zero placeholder in the format string
$#,##0.00 //this is what happens if the value is above zero
($#,##0.00) //this is for values below zero
Zero //literal "Zero" output if the value is equal to zero
EDIT - reading about custom numeric format strings from the link in @Jon's comment confirms that the three semicolon-delimited sections are indeed to specify formats for positive, negative, and zero values. 编辑 - 从@ Jon的注释中的链接读取自定义数字格式字符串 ,确认三个以分号分隔的部分确实指定了正值,负值和零值的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.