How to check if number is a decimal?

前端 未结 3 1868
陌清茗
陌清茗 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
    }
    
    0 讨论(0)
  • 2020-12-21 03:02

    You can also check if the string contains a ..

    String.parseString(decimalNumber).contains(".");
    
    0 讨论(0)
  • 2020-12-21 03:17

    Use the modulus operator to check if there is a remainder.

    if(a % b != 0) Log.v("result", "The result is a decimal");
    else Log.v("result", "The result is an integer");
    
    0 讨论(0)
提交回复
热议问题