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

One Response

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

Your email address will not be published. Required fields are marked *