C# Check if a decimal has more than 3 decimal places?

后端 未结 13 1864
一生所求
一生所求 2020-12-03 14:04

I have a situation that I cannot change: one database table (table A) accepts 6 decimal places, while a related column in a different table (table B) only has 3 decimal plac

13条回答
  •  猫巷女王i
    2020-12-03 14:42

    This works for 3 decimal places, and it can be adapted for a generic solution:

    static bool LessThan3DecimalPlaces(decimal dec)
    {
        decimal value = dec * 1000;
        return value == Math.Floor(value);
    }
    static void Test()
    {
        Console.WriteLine(LessThan3DecimalPlaces(1m * 0.00025m));
        Console.WriteLine(LessThan3DecimalPlaces(4000m * 0.00025m));
    }
    

    For a real generic solution, you'll need to "deconstruct" the decimal value in its parts - take a look at Decimal.GetBits for more information.

    Update: this is a simple implementation of a generic solution which works for all decimals whose integer part is less than long.MaxValue (you'd need something like a "big integer" for a trully generic function).

    static decimal CountDecimalPlaces(decimal dec)
    {
        Console.Write("{0}: ", dec);
        int[] bits = Decimal.GetBits(dec);
        ulong lowInt = (uint)bits[0];
        ulong midInt = (uint)bits[1];
        int exponent = (bits[3] & 0x00FF0000) >> 16;
        int result = exponent;
        ulong lowDecimal = lowInt | (midInt << 32);
        while (result > 0 && (lowDecimal % 10) == 0)
        {
            result--;
            lowDecimal /= 10;
        }
    
        return result;
    }
    
    static void Foo()
    {
        Console.WriteLine(CountDecimalPlaces(1.6m));
        Console.WriteLine(CountDecimalPlaces(1.600m));
        Console.WriteLine(CountDecimalPlaces(decimal.MaxValue));
        Console.WriteLine(CountDecimalPlaces(1m * 0.00025m));
        Console.WriteLine(CountDecimalPlaces(4000m * 0.00025m));
    }
    

提交回复
热议问题