java float string format code example

Example 1: test if string is float java

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
    }
}

Example 2: string to float java

String output = Float.parseFloat(floatNum); /** convert float to string using objName.parse(ObjName) <--capitalized usually. */

Tags:

C Example