Note that there are some explanatory texts on larger screens.

plurals
  1. PODecimal to string with thousand's separators?
    primarykey
    data
    text
    <p>Consider a <code>Decimal</code> value:</p> <pre><code>Decimal value = -1234567890.1234789012M; </code></pre> <p>i want to convert this <code>Decimal</code> value to a string, and include <em>"thousands separators"</em>. </p> <blockquote> <p><strong>Note:</strong> i don't want to include <strong>thousand's separators</strong>, i want to include <strong>digit grouping</strong>. The difference is important for cultures that don't group numbers into thousands, or don't use commas to separate groups</p> </blockquote> <p>Some example output with different standard formatting strings, on my computer, with my current locale:</p> <pre><code>value.ToString() = -1234567890..1234789012 (Implicit General) value.ToString("g") = -1234567890..1234789012 (General) value.ToString("d") = FormatException (Decimal whole number) value.ToString("e") = -1..234568e++009 (Scientific) value.ToString("f") = -1234567890..123 (Fixed Point) value.ToString("n") = -12,,3456,,7890..123 (Number with commas for thousands) value.ToString("r") = FormatException (Round trippable) value.ToString("c") = -$$12,,3456,,7890..123 (Currency) value.ToString("#,0.#") = -12,,3456,,7890..1 </code></pre> <p>What i <strong>want</strong> (depending on culture) is:</p> <pre><code>en-US -1,234,567,890.1234789012 ca-ES -1.234.567.890,1234789012 gsw-FR -1 234 567 890,1234789012 (12/1/2012: fixed gws-FR to gsw-FR) 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) (some debate if numbers should be "1,234,567,890") qps-PLOC 12,,3456,,7890..1234789012 </code></pre> <p>How can i convert a <code>Decimal</code> to a string, with digit groupings?</p> <hr> <p><strong>Update</strong>: Some more <em>desired</em> output, using my current culture of :</p> <pre><code>-1234567890M --&gt; -12,,3456,,7890 -1234567890.1M --&gt; -12,,3456,,7890..1 -1234567890.12M --&gt; -12,,3456,,7890..12 -1234567890.123M --&gt; -12,,3456,,7890..123 -1234567890.1234M --&gt; -12,,3456,,7890..1234 -1234567890.12347M --&gt; -12,,3456,,7890..12347 -1234567890.123478M --&gt; -12,,3456,,7890..123478 -1234567890.1234789M --&gt; -12,,3456,,7890..1234789 -1234567890.12347890M --&gt; -12,,3456,,7890..1234789 -1234567890.123478901M --&gt; -12,,3456,,7890..123478901 -1234567890.1234789012M --&gt; -12,,3456,,7890..1234789012 </code></pre> <hr> <p><strong>Update</strong>: i tried peeking at how <code>Decimal.ToString()</code> manages to use the <strong>General</strong> format to show all the digits that it needs to show:</p> <pre><code>public override string ToString() { return Number.FormatDecimal(this, null, NumberFormatInfo.CurrentInfo); } </code></pre> <p>except that <code>Number.FormatDecimal</code> is hidden somewhere:</p> <pre><code>[MethodImpl(MethodImplOptions.InternalCall)] public static extern string FormatDecimal(decimal value, string format, NumberFormatInfo info); </code></pre> <p>So that's a dead end.</p>
    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.
 

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