Format the string in a currency with a negative currency, for example, $ (10.00)

I need to format a negative currency as follows: $(10.00)

I tried using string.Format("{0:C}", itemprice) , but this gives me this result ($10.00) ($ inside the parenthesis

I also tried

 string fmt = "##;(##)"; itemprice.ToString(fmt); 

but he gives me the same thing as before ($10.00)

Any idea on how to get this result: $(10.00) .

+7
source share
3 answers
 itemPrice.ToString(@"$#,##0.00;$\(#,##0.00\)"); 

Must work. I just tested it on PowerShell:

 PS C:\Users\Jcl> $teststring = "{0:$#,##0.00;$\(#,##0.00\)}" PS C:\Users\Jcl> $teststring -f 2 $2,00 PS C:\Users\Jcl> $teststring -f -2 $(2,00) 

Is this what you want?

+5
source

Use the Jcl solution and make it a nice extension:

 public static string ToMoney(this object o) { return o.toString("$#,##0.00;$\(#,##0.00\)"); } 

Then just name it:

 string x = itemPrice.ToMoney(); 

Or another very simple implementation:

 public static string ToMoney(this object o) { // note: this is obviously only good for USD return string.Forma("{0:C}", o).Replace("($","$("); } 
+3
source

You will have to manually split this, as this is custom formatting.

 string.Format("{0}{1:n2}", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol, itemprice); 
+2
source

All Articles