解析十进制并在右边过滤额外的0?

Pri*_*rix 15 c# string decimal

从XML文件中我收到格式的小数:

1.132000
6.000000
Run Code Online (Sandbox Code Playgroud)

目前我正在使用Decimal.Parse,如下所示:

decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 30

我不认为有任何标准的数字格式字符串总是会省略尾随无关紧要的零,我担心.

您可以尝试编写自己的十进制规范化方法,但这可能非常棘手.使用BigInteger.NET 4中的类是合理可行的,但没有它(或类似的东西),它确实非常难.

编辑:好的,我想这就是你想要的:

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]) << 32) +
                (new BigInteger((uint) bits[2]) << 64);

            int sign = bits[3] & int.MinValue;            
            int exponent = (bits[3] & 0xff0000) >> 16;

            // The loop condition here is ugly, because we want
            // to do both the DivRem part and the exponent check :(
            while (exponent > 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 << 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 & UInt32Mask);
            mantissa >>= 32;
            bits[1] = (int) (uint) (mantissa & UInt32Mask);
            mantissa >>= 32;
            bits[2] = (int) (uint) (mantissa & 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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上有一种格式就是这样:toString("G29") (4认同)

sha*_*p00 7

这将从小数中删除所有尾随零,然后您可以使用ToString().

public static class DecimalExtensions
{
    public static Decimal Normalize(this Decimal value)
    {
        return value / 1.000000000000000000000000000000000m;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果你想要精确的精确度,比如5个小数位,首先是Normalize()然后再乘以1.00000m.