How to check whether input value is integer or float?

前端 未结 8 1037
庸人自扰
庸人自扰 2020-12-13 05:44

How to check whether input value is integer or float?

Suppose 312/100=3.12 Here i need check whether 3.12 is a float value or integer value, i.e., without any decimal

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 06:26

    The ceil and floor methods will help you determine if the number is a whole number.

    However if you want to determine if the number can be represented by an int value.

    if(value == (int) value)
    

    or a long (64-bit integer)

    if(value == (long) value)
    

    or can be safely represented by a float without a loss of precision

    if(value == (float) value)
    

    BTW: don't use a 32-bit float unless you have to. In 99% of cases a 64-bit double is a better choice.

提交回复
热议问题