gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Casting string to float in java
- User
Casting string to float in java
- Assistant
To cast a string to a float in Java, you can use the Float.parseFloat() method.
Here's an example:
String str = "3.14"; float f = Float.parseFloat(str); System.out.println(f); // prints 3.14The parseFloat() method takes a string argument representing the float value you want to parse. If the string can be converted to a float, the method returns the float value; otherwise, it throws a NumberFormatException.