Remove trailing zeros

后端 未结 18 965
天命终不由人
天命终不由人 2020-11-22 11:26

I have some fields returned by a collection as

2.4200
2.0044
2.0000

I want results like

2.42
2.0044
2

I t

相关标签:
18条回答
  • 2020-11-22 12:04

    Is it not as simple as this, if the input IS a string? You can use one of these:

    string.Format("{0:G29}", decimal.Parse("2.0044"))
    
    decimal.Parse("2.0044").ToString("G29")
    
    2.0m.ToString("G29")
    

    This should work for all input.

    Update Check out the Standard Numeric Formats I've had to explicitly set the precision specifier to 29 as the docs clearly state:

    However, if the number is a Decimal and the precision specifier is omitted, fixed-point notation is always used and trailing zeros are preserved

    Update Konrad pointed out in the comments:

    Watch out for values like 0.000001. G29 format will present them in the shortest possible way so it will switch to the exponential notation. string.Format("{0:G29}", decimal.Parse("0.00000001",System.Globalization.CultureInfo.GetCultureInfo("en-US"))) will give "1E-08" as the result.

    0 讨论(0)
  • 2020-11-22 12:05

    This is simple.

    decimal decNumber = Convert.ToDecimal(value);
            return decNumber.ToString("0.####");
    

    Tested.

    Cheers :)

    0 讨论(0)
  • 2020-11-22 12:08

    Trying to do more friendly solution of DecimalToString (https://stackoverflow.com/a/34486763/3852139):

    private static decimal Trim(this decimal value)
    {
        var s = value.ToString(CultureInfo.InvariantCulture);
        return s.Contains(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator)
            ? Decimal.Parse(s.TrimEnd('0'), CultureInfo.InvariantCulture)
            : value;
    }
    
    private static decimal? Trim(this decimal? value)
    {
        return value.HasValue ? (decimal?) value.Value.Trim() : null;
    }
    
    private static void Main(string[] args)
    {
        Console.WriteLine("=>{0}", 1.0000m.Trim());
        Console.WriteLine("=>{0}", 1.000000023000m.Trim());
        Console.WriteLine("=>{0}", ((decimal?) 1.000000023000m).Trim());
        Console.WriteLine("=>{0}", ((decimal?) null).Trim());
    }
    

    Output:

    =>1
    =>1.000000023
    =>1.000000023
    =>
    
    0 讨论(0)
  • 2020-11-22 12:11

    In case you want to keep decimal number, try following example:

    number = Math.Floor(number * 100000000) / 100000000;
    
    0 讨论(0)
  • 2020-11-22 12:12

    A very low level approach, but I belive this would be the most performant way by only using fast integer calculations (and no slow string parsing and culture sensitive methods):

    public static decimal Normalize(this decimal d)
    {
        int[] bits = decimal.GetBits(d);
    
        int sign = bits[3] & (1 << 31);
        int exp = (bits[3] >> 16) & 0x1f;
    
        uint a = (uint)bits[2]; // Top bits
        uint b = (uint)bits[1]; // Middle bits
        uint c = (uint)bits[0]; // Bottom bits
    
        while (exp > 0 && ((a % 5) * 6 + (b % 5) * 6 + c) % 10 == 0)
        {
            uint r;
            a = DivideBy10((uint)0, a, out r);
            b = DivideBy10(r, b, out r);
            c = DivideBy10(r, c, out r);
            exp--;
        }
    
        bits[0] = (int)c;
        bits[1] = (int)b;
        bits[2] = (int)a;
        bits[3] = (exp << 16) | sign;
        return new decimal(bits);
    }
    
    private static uint DivideBy10(uint highBits, uint lowBits, out uint remainder)
    {
        ulong total = highBits;
        total <<= 32;
        total = total | (ulong)lowBits;
    
        remainder = (uint)(total % 10L);
        return (uint)(total / 10L);
    }
    
    0 讨论(0)
  • 2020-11-22 12:12

    Depends on what your number represents and how you want to manage the values: is it a currency, do you need rounding or truncation, do you need this rounding only for display?

    If for display consider formatting the numbers are x.ToString("")

    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx and

    http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

    If it is just rounding, use Math.Round overload that requires a MidPointRounding overload

    http://msdn.microsoft.com/en-us/library/ms131274.aspx)

    If you get your value from a database consider casting instead of conversion: double value = (decimal)myRecord["columnName"];

    0 讨论(0)
提交回复
热议问题