Float to String Conversion in Java

In this article, we are going to see how we can convert float data type to String data type in java. there are many ways available for the float to string conversion in java, which are mentioned below –

Float to String conversion in java

By using Float.toString() method

Float.toString() is a static method of Float class, which is used to covert float data type to string data type.

float number = 459.99f;
String str = Float.toString(number);

A complete java program for float to string conversion using Float.toString() method.

package gangforcode;
public class floatToString {
    public static void main(String[] args) {
        float number = 459.99f;
        String str = Float.toString(number);
        System.out.println(str+ " this a string converted from float");
    }
}

By using String.valueOf()

By using this method we can also convert float value to string value. with help of String.valueOf() we can also convert int, double, long, etc into String data type.

float number = 459.99f;
String str = String.valueOf(number);

A complete java program for float to string conversion String.valueOf() method.

package gangforcode;
public class floatToString {
    public static void main(String[] args) {
        float number = 459.99f;
        String str = String.valueOf(number);
        System.out.println(str+ " this a string converted from float");
    }
}

output for float to string conversion

float to string conversion

Here we are able to perform string concatenation with float value because we already converted float to string before performing string concatenation.

String to float conversion –

Similarly we can convert String data type to float data type. For more information you can check here https://gangforcode.com/how-to-convert-string-to-float-in-java/

Similar Java Tutorial

Hello World – https://gangforcode.com/java-code-to-print-hello-world/
data types in java – https://gangforcode.com/data-types-in-java/
identifiers in java – https://gangforcode.com/identifiers-in-java/
keywords in java – https://gangforcode.com/keywords-in-java/
Multidimensional Array in Java – https://gangforcode.com/multidimensional-array-in-java/
Matrix Addition in Java – https://gangforcode.com/matrix-addition-in-java/
Binary Search in Java – https://gangforcode.com/binary-search-in-java/
first non repeating character – https://gangforcode.com/fiirst-non-repeating-character-in-the-string/

Leave a Comment