简体   繁体   中英

check if string contains number larger than a number

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

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
}

Use Float.valueOf . Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM