Java Float toString Method

JavaJavaBeginner
Practice Now

Introduction

The Java Float class is used to wrap a value of primitive types float, into an object. The toString() method of the Float class is used to convert a float type into a String type. This lab will demonstrate how to use the toString() method to convert a float value into a string.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117686{{"`Java Float toString Method`"}} java/classes_objects -.-> lab-117686{{"`Java Float toString Method`"}} java/class_methods -.-> lab-117686{{"`Java Float toString Method`"}} java/modifiers -.-> lab-117686{{"`Java Float toString Method`"}} java/oop -.-> lab-117686{{"`Java Float toString Method`"}} java/packages_api -.-> lab-117686{{"`Java Float toString Method`"}} java/user_input -.-> lab-117686{{"`Java Float toString Method`"}} java/wrapper_classes -.-> lab-117686{{"`Java Float toString Method`"}} java/identifier -.-> lab-117686{{"`Java Float toString Method`"}} java/arrays -.-> lab-117686{{"`Java Float toString Method`"}} java/comments -.-> lab-117686{{"`Java Float toString Method`"}} java/data_types -.-> lab-117686{{"`Java Float toString Method`"}} java/operators -.-> lab-117686{{"`Java Float toString Method`"}} java/output -.-> lab-117686{{"`Java Float toString Method`"}} java/strings -.-> lab-117686{{"`Java Float toString Method`"}} java/variables -.-> lab-117686{{"`Java Float toString Method`"}} java/object_methods -.-> lab-117686{{"`Java Float toString Method`"}} java/string_methods -.-> lab-117686{{"`Java Float toString Method`"}} java/system_methods -.-> lab-117686{{"`Java Float toString Method`"}} end

Create a java file

Create a java file with name FloatToStringDemo.java in the ~/project directory:

cd ~/project
touch FloatToStringDemo.java

Write Java code

Add the following Java code to the FloatToStringDemo.java file:

public class FloatToStringDemo {
    public static void main(String[] args) {
        //creating float variables
        float f1 = 2.34f;

        //converting float to string
        String str = Float.toString(f1);

        //printing float variables and converted string
        System.out.println("Float Value : " + f1);
        System.out.println("String Value : " + str);
    }
}

Compile the code

To compile the code, open a terminal, cd into the directory where the FloatToStringDemo.java file is located and run the following command:

javac FloatToStringDemo.java

Run the code

To run the code, use the following command:

java FloatToStringDemo

This will print both the float value and the String value on the console.

User input example

Add the following Java code to the FloatToStringDemo.java file to get user input and demonstrate the toString() method:

import java.util.Scanner;

public class FloatToStringDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //getting float number input from user
        System.out.print("Enter a float number: ");
        float f = sc.nextFloat();

        //converting float to string
        String str = Float.toString(f);

        //printing both float and converted string
        System.out.println("Float Value : " + f);
        System.out.println("String Value : " + str);
    }
}

Compile and run code

To compile and run the code, use the following commands:

javac FloatToStringDemo.java
java FloatToStringDemo

This will fulfill the user input example.

Summary

In this lab, you learned about the Java Float class and demonstrated how to use the toString() method to convert a float value into a String. By following step-by-step instructions, you learned how to write, compile, and run Java code.

Other Java Tutorials you may like