Convert float to string java – Java Program to Convert float to String

Convert float to string java: In the previous article we have discussed Java Program to Convert long to Int

In this article we will see how to convert a float to string.

Program To Convert float To String

Java convert float to string: Before going into the program let’s see some examples of float and String type.

Example-1: String types

int a = "b";
int b = "6.55";
Examples-2: float types

float a = 5.66;
float b = 57.54;

Let’s see different ways to Convert float to String.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

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

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

String.valueOf() is a method  which will simply typecasts below-given parameter to strings always. It is an inbuilt method of String class in java.

Approach :

  1. Take a float value  and store it in a long variable input1
  2. Then pass that input1 variable as parameter to String.valueOf( ) method which will convert the float to string value and return it.
  3. Store that string 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 float value through scanner class
    System.out.print("Enter a float value  : ");
    float input1=sc.nextFloat();
    // converting to string
    String output = String.valueOf(input1);
    System.out.println("Converted String is :"+output);
    }

}
Output : 

Enter a float value  : 4.55
Converted String is :4.55

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

Java float to string: float  can be converted to string by using  toString()  method, lets see how it actually works.

Whenever we use print statement in java,  toString() method of Object class in java is always called. toString() method of Object Class in java is always called by directly or indirectly. Here we are using this directly to convert the long to string itself .

Approach :

  1. Take a float value  and store it in a long variable input1
  2. Then pass that input1 variable as parameter to Float.toString( ) method which will convert the float to string value and return it .
  3. Store that string 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 float value through scanner class
        System.out.print("Enter a float value  : ");
        float input1=sc.nextFloat();
        // converting to string
        String output = Float.toString(input1);
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a float value  : 4.55
Converted String is :4.55

Method 3 : Java Program to Convert float to String Using  “+” operator

float can be converted to string by using  “+” operator,  let’s see how it will work.

Approach :

  1. Take a float value and store it in a float variable input1
  2. Take a string variable and concatenate “+” with the input variable which will treated as string .
  3. 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 float value through scanner class
        System.out.print("Enter a float value  : ");
        float input1=sc.nextFloat();
        // converting to string
        String output = " " + input1;
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a float value  : 4.55
Converted String is :4.55

Method 4 : Java Program to Convert float to String Using format() method

float can be converted to string by using format() method, let’s see how it actually works.

Approach :

  1. Take a float value and store it in an float variable input1
  2. Then pass that input1 variable as parameter to String.format ( ) method which will convert the float  to string value and return it .
  3. Store that string 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 float value through scanner class
        System.out.print("Enter a float value  : ");
        float input1=sc.nextFloat();
        // converting to string
        String output =String.format("%f", input1);
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a float value  : 4.55
Converted String is :4.55

Don’t miss the chance of Java programs examples with output pdf free download as it is very
essential for all beginners to experienced programmers for cracking the interviews.

Related Java Program: