Search code examples
c#string-formatting

Why does String.Format converts minus to parenthesis


I am using String.Format to add $ sign into values.

 var result = string.Format("{0:C}", Convert.ToDecimal(amount));

when amount is negative it converts minus sign into parenthesis- like below

-11.00 --> ($11.00)

I need this

-11.00 --> -$11.00

How can I fix this ?


Solution

  • Parentheses is the preferred accounting format to display negative currency, in the US at least. See this UX.SE post.

    If you don't want to follow that convention, you can create your own NumberFormatInfo:

    var format = CultureInfo.CreateSpecificCulture("en-US").NumberFormat;
    
    // "1" means "-$n"
    format.CurrencyNegativePattern = 1;
    
    string.Format(format, "{0:C}", -1m) // "-$1.00"
    

    See this for a list of the values that you can put in CurrencyNegativePattern.