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

后端 未结 13 1827
一生所求
一生所求 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条回答
  •  离开以前
    2020-12-03 14:38

    One more option based on @RodH257's solution, but reworked as an extension method:

    public static bool HasThisManyDecimalPlacesOrLess(this decimal value, int noDecimalPlaces)
    {
        return (Decimal.Round(value, noDecimalPlaces) == value);
    }
    

    You can then call that as:

    If !(tableA.Qty * tableA.Unit).HasThisManyDecimalPlacesOrLess(3)) return;
    

提交回复
热议问题