Reputation: 11926
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. For example if it returns 2000.33, then it is outputed as $2,003.33.
I know it is doing the formating, but how?
Thank you.
Upvotes: 2
Views: 462
Reputation: 4907
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.
Upvotes: 0
Reputation: 17546
Breaking down the format string: {0:$#,##0.00;($#,##0.00);Zero}
There are 3 groups:
$#,##0.00
- used when the argument (this.calcPayment(this._pv,1,2)
in this case) is positive.($#,##0.00)
- used when arg is negativeZero
- used when arg is zero#
is a digit placeholder and 0
is a zero placeholder (padding).
See this for more information.
Upvotes: 7
Reputation: 17451
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/
What's happening is the Format
method (function) is using the format string as a template, and then adding in your additional data provided.
Upvotes: 2