Advertisement
Advertisement


check if string contains number larger than a number


Question

i have a String in my app that receive by sms always changing and sometimes is a number with 2 decimal places and sometime without decimal places like below :

String sms = "12.02";
String sms = "105";

how can i Comparison number inside the String for example if larger than 5 do something in app ??

the decimal places not important

sorry for my poor language

2018/09/28
1
-1
9/28/2018 9:32:12 PM

Accepted Answer

Use Float.valueOf. Something like this:

if (Float.valueOf(sms)>5){
//do something
}
2018/09/28
1
9/28/2018 5:03:09 PM

If the decimal places are not important, you could do something like this:

String sms = "12.02";
double d = Double.parseDouble(sms); // 12.02 [approximately]
int i = (int) d; // 12

if (i > 5) {
    // do whatever
}
2018/11/17

try {
        if (Double.parseDouble(sms) > 5) {
                // do something
        }
    }
     catch (NumberFormatException e)
        {
            Log.e("Invalid Number",e.toString());
        }
2018/09/28