Hari Gillala
Hari Gillala

Reputation: 11926

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. 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

Answers (3)

Zann Anderson
Zann Anderson

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

Kashyap
Kashyap

Reputation: 17546

Breaking down the format string: {0:$#,##0.00;($#,##0.00);Zero}

There are 3 groups:

  1. $#,##0.00 - used when the argument (this.calcPayment(this._pv,1,2) in this case) is positive.
  2. ($#,##0.00) - used when arg is negative
  3. Zero - used when arg is zero

# is a digit placeholder and 0 is a zero placeholder (padding).

See this for more information.

Upvotes: 7

Jonathan M
Jonathan M

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

Related Questions