Signup/Sign In
LAST UPDATED: SEPTEMBER 1, 2020

Java Float toString() Method

Java toString() method is a part of the Float class of the java.lang package. This method returns the String object with its value equivalent to the Float Object.

In short, this method is used to convert Float Object into String.

Syntax:

public String toString()  

Parameters:

No parameters are passed in this method.

Returns:

Returns the String representation of the Float Object.

Example 1:

Here, the Float Objects are converted into its equivalent String representations.

import java.lang.Float;

public class StudyTonight
{  
    public static void main(String[] args) 
      {  
        Float a = 50.67f;       
        String s1 = a.toString();  // return a string value 
        System.out.println("Equivalent String is " + s1);              
        
        Float b = -18.80f;            
        String s2 = b.toString();  // return a string value 
        System.out.println("Equivalent String is " + s2);  
      }  
}  


Equivalent String is 50.67
Equivalent String is -18.8

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  

public class StudyTonight
{  
     public static void main(String[] args) 
     {  
          try
          {
              System.out.print("Enter the value ");  
              Scanner sc = new Scanner(System.in);  
              Float val = sc.nextFloat();  
              String s = val.toString();   //converting to string
              System.out.println("String value is : "+ s);  
          }
          catch(Exception e)
          {
              System.out.println("Invalid input!!");
          }
     }  
}  


Enter the value 85.98
String value is : 85.98
****************************
Enter the value -69.43
String value is : -69.43
****************************
Enter the value 0x590
Invalid input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.