Set Negative and Zero Formatting in String.Format

Apart from setting the format you want the string to be shown, the format parameter in the String.Format method also allows to pass the negative formatting and zero formatting.

First is an example of the normal usage of this method:

String.Format(“{0:€ #,###.##}”, price);

To include the negative formatting add the following:

String.Format(“{0:€ #,###.##; (€ #,###.##)}”, price);

To include both the negative and zero formatting:

String.Format(“{0:€ #,##0.00; (€ #,###.##); Zero}”, price);

Other examples of negative formatting can include also html elements like the following:

String.Format(“{0:€ #,##0.00;<span style=’color:red’>-€ #,##0.00</span>}”, price);

Happy Formatting!! 🙂

References: click here

Advertisements

One Response to “Set Negative and Zero Formatting in String.Format”

  1. hfrmobile Says:

    Nice article!

    Tried it with C# 2.0 and works fine.

    Since which C# version is negative an zero formatting possible? Never saw this in MSDN etc.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: