Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Am I right in saying it's putting it in brackets, i.e. it's formatting it as <code>($1,234.00)</code> ? If so, I believe that's the intended behaviour for the US.</p> <p>However, you can create your own <code>NumberFormatInfo</code> which doesn't behave this way. Take an existing <code>NumberFormatInfo</code> which is "mostly right", call <code>Clone()</code> to make a mutable copy, and then set the <a href="http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern.aspx" rel="noreferrer"><code>CurrencyNegativePattern</code></a> appropriately (I think you want value 2).</p> <p>For example:</p> <pre><code>using System; using System.Globalization; class Test { static void Main() { var usCulture = CultureInfo.CreateSpecificCulture("en-US"); var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone(); clonedNumbers.CurrencyNegativePattern = 2; string formatted = string.Format(clonedNumbers, "{0:C2}", -1234); Console.WriteLine(formatted); } } </code></pre> <p>This prints $-1,234.00. If you actually want exactly $-1234, you'll need to set the <a href="http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx" rel="noreferrer"><code>CurrencyGroupSizes</code></a> property to <code>new int[]{0}</code> and use <code>"{0:C0}"</code> instead of <code>"{0:C2}"</code> as the format string.</p> <p>EDIT: Here's a helper method you can use which basically does the same thing:</p> <pre><code>private static readonly NumberFormatInfo CurrencyFormat = CreateCurrencyFormat(); private static NumberFormatInfo CreateCurrencyFormat() { var usCulture = CultureInfo.CreateSpecificCulture("en-US"); var clonedNumbers = (NumberFormatInfo) usCulture.NumberFormat.Clone(); clonedNumbers.CurrencyNegativePattern = 2; return clonedNumbers; } public static string FormatCurrency(decimal value) { return value.ToString("C2", CurrencyFormat); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload