Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


1 Answers

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

like image 111
Adolfo Perez Avatar answered Aug 19 '26 03:08

Adolfo Perez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!