I have the following scenario. A decimal value needs to be displayed as a currency but include the appropriate currency symbol and sign, thus:
- -45.23 is displayed as -£45.23
- 45.23 is displayed as +£45.23
The currency sign must come from System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol. I have this, but I can’t find a way to swap the sign and currency symbol:
string s1 = string.Format(@"{1}{0:+#,##0.00;-#,##0.00}", 45.09M,
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
Console.WriteLine(s2);
string s2 = string.Format(@"{1}{0:+#,##0.00;-#,##0.00}", -45.09M,
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
Console.WriteLine(s2);
Outputs:
£+45.09
£-45.09
I want:
+£45.09
-£45.09
UPDATE
Someone posted another answer regarding the NumberFormatInfo.CurrencyPositivePattern Property, which looked promising, especially since the but it appears that Microsoft overlooked the possibility that the NumberFormatInfo.CurrencyNegativePattern Property contains various options for the presentation of the negative sign. However, the NumberFormatInfo.CurrencyPositivePattern does not include an option to include the positive sign! Damn. Thanks to whoever it was that mentioned it, but deleted their comments. I learnt someone nevertheless.
Your
string.Formatis explicitly putting the +/- at the front.If you split the operation into two steps, it becomes easier. First inject your currentcy symbol, then format your number.