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

后端 未结 13 1861
一生所求
一生所求 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:43

    The basics is to know how to test if there are decimal places, this is done by comparing the value to its rounding

    double number;
    bool hasDecimals = number == (int) number;
    

    Then, to count 3 decimal places, you just need to do the same for your number multiplied by 1000:

    bool hasMoreThan3decimals = number*1000 != (int) (number * 1000)
    

提交回复
热议问题