Java Program to Convert String to float

In the previous article we have discussed Java Program to Convert String to long

In this article we will see how to convert String to float.

Program to Convert String to float

Before going into the program, let’s see some examples of both String and float type.

Example-1: String types

String a = "b";
String b = "3.333";
Examples-2: float types

float a =3.333;
float b = 3;

Let’s see different method to do it.

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Method 1 : Java Program to Convert String to float Using parseFloat() method

String type variable can be converted to float by using  parseFloat() see how it actually works.

Here this method is  a wrapper class in Java. This method of the Boolean class  converts the string variables into float.

Approach :

  1. Take a string value and store it in a string   variable input1.
  2. Then pass that input1 variable as parameter to parseFloat() method which will convert the String into float value and return it .
  3. Store that float value in a variable output .
  4. Display the result.

Program:

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a String value through scanner class
        System.out.print("Enter a string : ");
        String input1=sc.next();
        // converting to Float
        float output =Float.parseFloat(input1) ;
        System.out.println("Converted Float value is : " + output);
    }
}
Output : 

Enter a string : 5.66
Converted Float value is : 5.66

Method 2 : Java Program to Convert String to float Using valueOf() method

String type variable can be converted to float by using  valueOf() method, let’s see how it actually works.

This method returns an object of the float class. However, the object will be automatically converted into the primitive type.

 Approach :

  1. Take a string value and store it in a string   variable input1.
  2. Then pass that input1 variable as parameter to valueOf() method which will convert the String into float value and return it .
  3. Store that float value in a variable output .
  4. Display the result.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a string value through scanner class
        System.out.print("Enter a string : ");
        String input1=sc.next();
        // converting to Float
        float output =Float.valueOf(input1) ;
        System.out.println("Converted Float value is : " + output);
    }
}
Output : 

Enter a string : 5.66
Converted Float value is : 5.66

Grab the opportunity to learn all effective java programming language concepts from basic to
advance levels by practicing these Java Program Examples with Output

Related Java Program: