c# - 解析小数并过滤右边多余的0?

标签 c# string decimal

我从一个 XML 文件中收到以下格式的小数:

1.132000
6.000000

目前我正在像这样使用 Decimal.Parse:

decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture);
  • 如何将 myDecimal 打印成字符串 看起来像下面?

    1.132
    6
    

最佳答案

我不认为有任何标准的数字格式字符串总是会忽略尾随的无意义的零,恐怕。

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

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

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());
        }
    }
}

关于c# - 解析小数并过滤右边多余的0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298719/

相关文章:

c# - NLog 自定义 LayoutRenderer 用于范围缩进

c# - 你能捕捉到 RaceOnRCWCleanup 异常吗

c# - 开发需要哪些Android SDK包?

c - 将字符串添加到字符串数组会以某种方式更改数组中的先前字符串

c# - 如何使小数变量保留/显示小数位,即使小数位仅为零?

c# - asp.net MapPageRoute加载页面两次

r - R 中的字符串模式

java - 使用其他语言而不是英语作为java字符串(以波斯语为例)

java长整顿到小数点后两位绝对值

c - 如何从 strlen 返回中减去十六进制然后添加到十六进制地址?