Convert Float to String

JavaJavaBeginner
Practice Now

Introduction

In Java, there are multiple ways to convert a float to a string. In this lab, we will discuss three different methods of converting Float to String in Java. The three methods that will be covered are: using the valueOf() method of the String class, using the toString() method of the Float class, and using string literals.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") 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/wrapper_classes -.-> lab-117422{{"`Convert Float to String`"}} java/identifier -.-> lab-117422{{"`Convert Float to String`"}} java/data_types -.-> lab-117422{{"`Convert Float to String`"}} java/operators -.-> lab-117422{{"`Convert Float to String`"}} java/output -.-> lab-117422{{"`Convert Float to String`"}} java/strings -.-> lab-117422{{"`Convert Float to String`"}} java/variables -.-> lab-117422{{"`Convert Float to String`"}} java/object_methods -.-> lab-117422{{"`Convert Float to String`"}} java/string_methods -.-> lab-117422{{"`Convert Float to String`"}} java/system_methods -.-> lab-117422{{"`Convert Float to String`"}} end

Declare the Float Value

Declare and initialize a float variable named floatValue with a value of 10.5.

float floatValue = 10.5f;

Convert Float to String using valueOf() Method

Use the valueOf() method of the String class to convert the floatValue to a string.

String strValue = String.valueOf(floatValue);

Print the String Value

Print the strValue and its datatype.

System.out.println(strValue);
System.out.println("Data Type: " + strValue.getClass().getName());

Convert Float to String using toString() Method

Use the toString() method of the Float class to convert the floatValue to a string.

String strValue2 = Float.toString(floatValue);

Print the String Value

Print the strValue2 and its datatype.

System.out.println(strValue2);
System.out.println("Data Type: " + strValue2.getClass().getName());

Convert Float to String using String literals

Use the string literals to convert the floatValue to a string.

String strValue3 = "" + floatValue;

Print the String Value

Print the strValue3 and its datatype.

System.out.println(strValue3);
System.out.println("Data Type: " + strValue3.getClass().getName());

Compile and Run the Code

To compile the code use the following command:

javac FloatToString.java

To run the compiled code use this command:

java FloatToString

Summary

In this lab, we discussed three different methods to convert Float to String in Java. The conversion methods discussed were using the valueOf() method of the String class, using the toString() method of the Float class, and using string literals. By using these methods, a floating-point value can be converted into a string value in Java.

Other Java Tutorials you may like