Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

How to display 0 when value is 0 and in Format, # is Custom Specifier?

I was converting my integer value to string value by using "#" custom format like this

string str = string.Format("{0:###,###}", 10000);

So the result is "10,000".

But when value is 0 str is Empty.

string str = string.Format("{0:###,###}", 0);

I know this has been explained in MSDN, but I want to display 0 when value is zero. How to do that?

Upvotes: 0

Views: 3644

Answers (1)

Martin Liversage
Martin Liversage

Reputation: 106826

You can use ###,##0 (or #,##0) as the format string. This will display 0 when the value is zero. You may also want to consider the N0 format string.

To quote Custom Numeric Format Strings on MSDN:

"0": Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

"#": Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

Upvotes: 6

Related Questions