Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can specify a custom pattern (the pattern will appropriately resolve to the culture specific method of grouping and the appropriate grouping and decimal separator characters). A pattern can have <a href="http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SectionSeparator" rel="nofollow">positive, negative and zero sections</a>. The positive pattern is always the same but the negative pattern depends on the culture and can be retrieved from the NumberFormatInfo's <a href="http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbernegativepattern.aspx" rel="nofollow">NumberNegativePattern</a> property. Since you want as much precision as possible, you need to fill out 28 digit placeholders after the decimal; the comma forces grouping.</p> <pre><code> public static class DecimalFormatters { public static string ToStringNoTruncation(this Decimal n, IFormatProvider format) { NumberFormatInfo nfi = NumberFormatInfo.GetInstance(format); string[] numberNegativePatterns = { "(#,0.############################)", //0: (n) "-#,0.############################", //1: -n "- #,0.############################", //2: - n "#,0.############################-", //3: n- "#,0.############################ -"};//4: n - var pattern = "#,0.############################;" + numberNegativePatterns[nfi.NumberNegativePattern]; return n.ToString(pattern, format); } public static string ToStringNoTruncation(this Decimal n) { return n.ToStringNoTruncation(CultureInfo.CurrentCulture); } } </code></pre> <h2>Sample output</h2> <pre><code>Locale Output ======== ============================ en-US -1,234,567,890.1234789012 ca-ES -1.234.567.890,1234789012 hr-HR - 1.234.567.890,1234789012 gsw-FR -1 234 567 890,1234789012 fr-CH -1'234'567'890.1234789012 ar-DZ 1,234,567,890.1234789012- prs-AF 1.234.567.890,1234789012- ps-AF 1،234،567،890,1234789012- as-IN -1,23,45,67,890.1234789012 lo-LA (1234567,890.1234789012) qps-PLOC -12,,3456,,7890..1234789012 </code></pre> <p>There is currently no locale that uses <code>NegativeNumberFormat</code> 4 (<code>n -</code>), so that case cannot be tested. But there's no reason to think it would fail.</p>
 

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