Search code examples
currencystring.format

String format for currency(negative value formatted with braces)


When we convert like String.Format("{0:C}", 126.45) it returns $126.45

but if we convert like String.Format("{0:C}", -126.45) it returns ($126.45)

Why negative conversion return braces? What to do if we don't want this braces?


Solution

  • Why don't you try something like:

    String.Format("{0:$#,##0.00}", -126.45)
    

    According to the documentation here a format of "{0:C1}" or "{0:C2}" should work, but for some strange reason it is not..

    Another approach could be setting the CultureInfo:

    CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    culture.NumberFormat.CurrencyNegativePattern = 1;
    string s = string.Format(culture, "{0:c}", -126.45);
    

    Reference here