3

I have a string and I know that is only contained a number.

I want to check this number is int or float.

Ali
  • 115
  • 1
  • 2
  • 7
  • Any attempt from your side ? – Suresh Atta Apr 01 '17 at 11:10
  • Welcome to Stack Overflow! We are a question-and-answer site, not a coders-for-hire service. Please narrow your question down to a specific coding problem that would be on-topic for this site. See: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) and [How to ask a good question when I'm not sure what I'm looking for?](https://meta.stackoverflow.com/questions/262527/how-to-ask-a-good-question-when-im-not-sure-what-im-looking-for) – Joe C Apr 01 '17 at 11:10

3 Answers3

18

There are many ways to solve your problem for example you can use try{}catch(){}:

Solution 1

public static void main(String[] args) {
    String str = "5588";
    //check if int
    try{
        Integer.parseInt(str);
    }catch(NumberFormatException e){
        //not int
    }
    //check if float
    try{
        Float.parseFloat(str);
    }catch(NumberFormatException e){
        //not float
    }
}

Solution 2

Or you can use regex [-+]?[0-9]*\.?[0-9]+ :

boolean correct = str.matches("[-+]?[0-9]*\\.?[0-9]+");

For more details take a look at this Matching Floating Point Numbers with a Regular Expression

YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • The regex suggested as a solution is not valid. It does not accept negative sign, and it accepts weird strings like `3.` or even just `.` – jbx Jun 15 '18 at 21:21
  • @jbx this is correct and this was from a long time when I start learning regex I fix my mistake hope this can help more – YCF_L Jun 15 '18 at 21:30
  • Yes the new one looks good. `\\d` can still work instead of `[0-9]` but maybe in this case the intent is clearer (since it is parsing a number). – jbx Jun 19 '18 at 08:24
  • both `Integer.parseInt("1")` and `Float.parseFloat("1")` will not throw exception – 91StarSky May 22 '21 at 01:28
2

Late to the party . In Apache Commons NumberUtils class.

NumberUtils.isCreatable(String) which checks whether a String is a valid Java number or not.

    log.info("false {} ", NumberUtils.isCreatable("NA.A"));
    log.info("true {} ", NumberUtils.isCreatable("1223.589889890"));
    log.info("true {} ", NumberUtils.isCreatable("1223"));
    log.info("true {} ", NumberUtils.isCreatable("2.99e+8"));

Hope this helps someone.

PremKumarR
  • 421
  • 1
  • 6
  • 17
  • 1
    But this will not inform whether the String is `Int` or `Float` right? It will return `true` irrespective of it, right? This will inform only if its some sort of number. We need a another check to confirm if its Int or Float. – BATMAN_2008 Sep 16 '21 at 15:16
0
public static String getPrimitiveDataTypeForNumberString(String str) {
    try {
        if (str.matches("^[\\p{Nd}]+[Ll]$")) {
            str = str.substring(0, str.length() - 1);
            long l = Long.parseLong(str);
            if (l <= Long.MAX_VALUE) {
                return "Long";
            }
        } else if (str.matches("^[\\p{Nd}]+[.][\\p{Nd}Ee]+[Ff]$")) {
            str = str.substring(0, str.length() - 1);
            float f = Float.parseFloat(str);
            if (f <= Float.MAX_VALUE) {
                return "Float";
            }
        } else if (str.matches("^[\\p{Nd}]+[.][\\p{Nd}Ee]+[Dd]$")) {
            str = str.substring(0, str.length() - 1);
            double d = Double.parseDouble(str);
            if (d <= Double.MAX_VALUE) {
                return "Double";
            }
        } else if (str.matches("^[\\p{Nd}]+$")) {
            double d = Double.parseDouble(str);
            if (d <= Integer.MAX_VALUE) {
                return "Integer";
            } else if (d <= Long.MAX_VALUE) {
                return "Long";
            } else if(d <= Float.MAX_VALUE) {
                return "Float";
            } else if(d <= Double.MAX_VALUE) {
                return "Double";
            }
        } else if (str.matches("^[\\p{Nd}]+[.][\\p{Nd}Ee]+$")) {
            double d = Double.parseDouble(str);
            if (d > Float.MAX_VALUE) {
                return "Double";
            }
            return "Float";
        }
    } catch (NumberFormatException e) {
    }

    return "Unknown";
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 03 '22 at 20:34