No products in the cart.

Ask questions which are clear, concise and easy to understand.

Ask Question
  • 1 answers

Imran Khan 7 years, 9 months ago

         

 

Converting a String to a float requires that you first convert the String to a Float object, then convert the Float object to a float data type (Float is an object, while float is a primitive data type).

An example of a simple program that performs this conversion is shown below:

<pre> public class StringToFloat { public static void main (String[] args) { // String s = "fred"; // do this if you want an exception String s = "100.00"; try { float f = Float.valueOf(s.trim()).floatValue(); System.out.println("float f = " + f); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } } } </pre>

Other notes

  • Float.toString(float f) is used to convert in the other direction, from a float to a String.

myCBSEguide App

myCBSEguide

Trusted by 1 Crore+ Students

Test Generator

Test Generator

Create papers online. It's FREE.

CUET Mock Tests

CUET Mock Tests

75,000+ questions to practice only on myCBSEguide app

Download myCBSEguide App