Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx" rel="nofollow noreferrer"><code>ToString</code></a> method on decimals by default uses the CultureInfo.CurrentCulture for the user's session, and thus varies based on whom is running the code. </p> <p>The ToString method also accepts an IFormatProvider in various overloads. This is where you need to supply your culture-specific Formatters. </p> <p>For instance, if you pass the NumberFormat for fr-CH, you can format things as that culture expects: </p> <pre><code>var culture = CultureInfo.CreateSpecificCulture("fr-CH"); Decimal value = -1234567890.1234789012M; Console.WriteLine(value.ToString("##,#.###############", culture.NumberFormat)); </code></pre> <p>Will output</p> <pre><code> -1'234'567'890.1234789012 </code></pre> <p>Edit #3 - rewrote using custom formatters. This should do what you want based on the new updated question. </p> <p>Edit #4 - Took all of your input, and ran this: </p> <pre><code>public void TestOutput() { PrintValue(-1234567890M); PrintValue(-1234567890.1M); PrintValue(-1234567890.12M); PrintValue(-1234567890.123M); PrintValue(-1234567890.1234M); PrintValue(-1234567890.12347M); PrintValue(-1234567890.123478M); PrintValue(-1234567890.1234789M); PrintValue(-1234567890.12347890M); PrintValue(-1234567890.123478901M); PrintValue(-1234567890.1234789012M); } private static void PrintValue(decimal value) { var culture = CultureInfo.CreateSpecificCulture("qps-PLOC"); Console.WriteLine(value.ToString("##,#.###############", culture.NumberFormat)); } </code></pre> <p>Gives output matching what you supplied: </p> <pre><code>--12,,3456,,7890 --12,,3456,,7890..1 --12,,3456,,7890..12 --12,,3456,,7890..123 --12,,3456,,7890..1234 --12,,3456,,7890..12347 --12,,3456,,7890..123478 --12,,3456,,7890..1234789 --12,,3456,,7890..1234789 --12,,3456,,7890..123478901 --12,,3456,,7890..1234789012 </code></pre> <p>As pointed out by Joshua, this only works for some locales. </p> <p>From the looks of it then, you need to pick the lesser of two evils: Knowing the precision of your numbers, or specifying formats for each culture. I'd wager knowing the precision of your numbers may be easier. In which case, a previous version of my answer may be of use: </p> <p>To explicitly control the number of decimal places to output, you can clone the number format provided by the culture and modify the NumberDecimalDigits property. </p> <pre><code>var culture = CultureInfo.CreateSpecificCulture("fr-CH"); Decimal value = -1234567890.1234789012M; NumberFormatInfo format = (NumberFormatInfo)culture.NumberFormat.Clone(); format.NumberDecimalDigits = 30; Console.WriteLine(value.ToString("n", format)); </code></pre> <p>This outputs: </p> <pre><code>-1'234'567'890.123478901200000000000000000000 </code></pre>
 

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