How to check if number is a decimal?

前端 未结 3 1880
陌清茗
陌清茗 2020-12-21 02:42

I create android app for dividing numbers.

int a,b;

int result = a/b;

if (result==decimal){

Log.v (\"result\",\"your result number is decimal\")

} else {         


        
3条回答
  •  没有蜡笔的小新
    2020-12-21 02:50

    int will not hold a decimal, they always take the floor of the result: for example 3 / 5 = 0 as an int. That said you can use modulo (%) to determine if decimals are being dropped.

    if(a % b > 0) { // 3 % 5 = 3
        // Decimal places will be lost
    }
    

提交回复
热议问题