Int.ToString ("C") removes the negative character

According to http://msdn.microsoft.com/en-us/library/6t7dwaa5.aspx , if I want to display the number as a currency, I can call .ToString () and pass the parameter, in this case "C"

However, based on the details below that are on the MSDN website, it automatically removes the minus sign (-), which means that my value is now positive.

int value = -16325; // Display value using default ToString method. Console.WriteLine(value.ToString()); // Displays -16325 // Display value using some standard format specifiers. Console.WriteLine(value.ToString("G")); // Displays -16325 Console.WriteLine(value.ToString("C")); // Displays ($16,325.00) Console.WriteLine(value.ToString("D")); // Displays -16325 Console.WriteLine(value.ToString("F")); // Displays -16325.00 Console.WriteLine(value.ToString("N")); // Displays -16,325.00 Console.WriteLine(value.ToString("X")); // Displays FFFFC03B 

So, how do I save the minus sign, I have to use the Contains ("-") method, and if it returns, return it back.

Something like this code (apologizes if it is incorrectly encoded, but you understand this idea).

 string x = (value.ToString().Contains("-") : "-" + value.ToString("C") ? + value.ToString("C")); 
+8
tostring
source share
7 answers

It depends on the value of your CurrentCulture.NumberFormat.CurrencyNegativePattern . Possible values: from 0 to 15:

 0 : ($12,345.00) 1 : -$12,345.00 2 : $-12,345.00 3 : $12,345.00- 4 : (12,345.00$) 5 : -12,345.00$ 6 : 12,345.00-$ 7 : 12,345.00$- 8 : -12,345.00 $ 9 : -$ 12,345.00 10 : 12,345.00 $- 11 : $ 12,345.00- 12 : $ -12,345.00 13 : 12,345.00- $ 14 : ($ 12,345.00) 15 : (12,345.00 $) 

So it looks like you want to set it to 1 to get the result you are using, with something like:

 int value = -12345; var numberFormat = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone(); numberFormat.CurrencyNegativePattern = 1; string numberAsCurrency = value.ToString("C", numberFormat) 

Although, based on your comments that this is a remote server, and you always want to format it in a certain way, it might be better to just set the culture for the entire stream to a controlled value, and this will affect all subsequent ToString() calls this stream:

 int value = -12345; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyNegativePattern = 1; string numberAsCurrency = value.ToString("C"); 
+12
source share

You can create a custom NumberFormatInfo from your current locale. Then you can specify its CurrencyNegativePattern , for example:

 int value = -16325; var culture = CultureInfo.CurrentCulture; var mutableNfi = (NumberFormatInfo)culture.NumberFormat.Clone(); mutableNfi.CurrencyNegativePattern = 1; Console.WriteLine(value.ToString("C", mutableNfi)); // Displays (-$16,325.00) 
+7
source share

It depends on the localization settings of the machine. Negative currencies in many locales are represented as ( amount ) So .net uses this here. I am not sure how to override this. But here is what happens here.

+3
source share

It depends on the culture. It uses NumberFormatInfo.CurrencyNegativePattern - which in the US seems to use parentheses to indicate a negative number. (A positive number would not have parentheses.)

That way you can clone the current CultureInfo and change the CurrencyNegativePattern NumberFormatInfo associated with it, but I would be careful about that. In general, you should believe that the cultural information used by .NET is appropriate for this culture. You need to decide whether you want, for example, a culture-specific performance - if not, you can use an invariant culture.

+3
source share

you might like to try

  Console.WriteLine((-1234).ToString("c")); CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; CultureInfo newCulture = new CultureInfo(currentCulture.IetfLanguageTag); newCulture.NumberFormat.CurrencyNegativePattern = 1; Thread.CurrentThread.CurrentCulture = newCulture; Console.WriteLine((-1234).ToString("c")); 

MSDN: NumberFormatInfo.CurrencyNegativePattern property for details ...

+3
source share

You can use this:

 string text = value < 0 ? "-" + (-value).ToString("C") : value.ToString("C"); 

But make sure that the localization settings are not changed to prevent unpredictable results (for example, $ 1.00).

+1
source share

This method can be used to get a currency string, and only if it is enclosed in brackets, remove the brackets and add a "-".

 private string ToCurrencyString(decimal amount) { string c = amount.ToString("C"); if (c.StartsWith("(") && c.EndsWith(")")) { c = "-" + c.SubString(1, c.Length - 2); } return c; } 
0
source share

All Articles