Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there are any standard numeric format strings which will always omit trailing insignificant zeroes, I'm afraid.</p> <p>You could try to write your own decimal normalization method, but it could be quite tricky. With the <code>BigInteger</code> class from .NET 4 it would be reasonably feasible, but without that (or something similar) it would be very hard indeed.</p> <p>EDIT: Okay, I think this is what you want:</p> <pre><code>using System; using System.Numerics; public static class DecimalExtensions { // Avoiding implicit conversions just for clarity private static readonly BigInteger Ten = new BigInteger(10); private static readonly BigInteger UInt32Mask = new BigInteger(0xffffffffU); public static decimal Normalize(this decimal input) { unchecked { int[] bits = decimal.GetBits(input); BigInteger mantissa = new BigInteger((uint) bits[0]) + (new BigInteger((uint) bits[1]) &lt;&lt; 32) + (new BigInteger((uint) bits[2]) &lt;&lt; 64); int sign = bits[3] &amp; int.MinValue; int exponent = (bits[3] &amp; 0xff0000) &gt;&gt; 16; // The loop condition here is ugly, because we want // to do both the DivRem part and the exponent check :( while (exponent &gt; 0) { BigInteger remainder; BigInteger divided = BigInteger.DivRem(mantissa, Ten, out remainder); if (remainder != BigInteger.Zero) { break; } exponent--; mantissa = divided; } // Okay, now put it all back together again... bits[3] = (exponent &lt;&lt; 16) | sign; // For each 32 bits, convert the bottom 32 bits into a uint (which won't // overflow) and then cast to int (which will respect the bits, which // is what we want) bits[0] = (int) (uint) (mantissa &amp; UInt32Mask); mantissa &gt;&gt;= 32; bits[1] = (int) (uint) (mantissa &amp; UInt32Mask); mantissa &gt;&gt;= 32; bits[2] = (int) (uint) (mantissa &amp; UInt32Mask); return new decimal(bits); } } class Program { static void Main(string[] args) { Check(6.000m); Check(6000m); Check(6m); Check(60.00m); Check(12345.00100m); Check(-100.00m); } static void Check(decimal d) { Console.WriteLine("Before: {0} - after: {1}", d, d.Normalize()); } } } </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. 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