Convert string to float?

后端 未结 6 971
谎友^
谎友^ 2020-12-09 07:49

Just as title says. I don\'t believe it is possible to do this but if it is let me know.

This is needed for a bukkit (minecraft server) plugin I\'m writing. I want

相关标签:
6条回答
  • 2020-12-09 08:09

    Use Float.valueOf(String) to do the conversion.

    The difference between valueOf() and parseFloat() is only the return. Use the former if you want a Float (object) and the latter if you want the float number.

    0 讨论(0)
  • 2020-12-09 08:10

    Try this:

    String numberStr = "3.5";
    Float number = null;
    try {
       number = Float.parseFloat(numberStr);
    } catch (NumberFormatException e) {
        System.out.println("numberStr is not a number");
    }
    
    0 讨论(0)
  • 2020-12-09 08:15
    public class NumberFormatExceptionExample {
    private static final String str = "123.234";
    public static void main(String[] args){
    float i = Float.valueOf(str); //Float.parseFloat(str);
    System.out.println("Value parsed :"+i);
    }
    }
    

    This should resolve the problem.

    Can anyone suggest how should we handle this when the string comes in 35,000.00

    0 讨论(0)
  • 2020-12-09 08:23
    String s = "3.14";
    float f = Float.parseFloat(s);
    
    0 讨论(0)
  • 2020-12-09 08:29

    Using Float.parseFloat()?

    class Test {
        public static void main(String[] args) {
            String s = "3.14";
            float f = Float.parseFloat(s);
            System.out.println(f);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 08:29

    Try this:

    String yourVal = "20.5";
    float a = (Float.valueOf(yourVal)).floatValue(); 
    System.out.println(a);
    
    0 讨论(0)
提交回复
热议问题