1. Introduction
In this article, We'll learn how to convert Float values to String. This is a common scenario to the developers and how conversions can be done using Java API methods.
String and Float classes have been provided with utility methods and these methods are always defined as static because directly can be accessed with the class names.
All examples are designed to show the conversion of primitive float and wrapper float to String objects.
2. Float.toString() Example
Float is a wrapper class in java that wraps primitive type float in an Object and it has a method toString() to convert the string into float.
Syntax:
public String toString()
Example program to convert Float to String with Float.toString() Method:
Below program to convert primitive float to String object and Wrapper Float to String.
Below program to convert primitive float to String object and Wrapper Float to String.
package com.java.w3schools.blog.conversions; /** * * Example program to convert Float to String with Float.toString() Method. * * @author javaprogramto.com * */ public class FloatToStringExample { public static void main(String[] args) { // primitive float to string float primitiveFloat = 100f; String primitiveFloatStr = Float.toString(primitiveFloat); System.out.println("primitiveFloatStr :" + primitiveFloatStr); float pi = 3.14f; String piStringValue = Float.toString(pi); System.out.println("piStringValue : " + piStringValue); // Wrapper Float to string Float floatWrapper = new Float(1000.1000); String floatWrapperStr = Float.toString(floatWrapper); System.out.println("floatWrapperStr : " + floatWrapperStr); Float floatWrapperPI = new Float(3.14); String floatWrapperPIStr = Float.toString(floatWrapperPI); System.out.println("floatWrapperPIStr : " + floatWrapperPIStr); } }
Output:
primitiveFloatStr :100.0
piStringValue : 3.14
floatWrapperStr : 1000.1
floatWrapperPIStr : 3.14
3. String.valueOf() Example
The string is a representation of single or multiple characters. String class has a static method valueOf() which takes primitive float type value. valueOf(float f) method returns a String value represents the float value.
Syntax:
public static String valueOf(float f)
Example:
public class StringValueOfExample { public static void main(String[] args) { // primitive float to string float primitiveFloat = 900; String primitiveFloatStr = String.valueOf(primitiveFloat); System.out.println("primitiveFloatStr :" + primitiveFloatStr); float pi = 3.14000f; String piStringValue = String.valueOf(pi); System.out.println("piStringValue : " + piStringValue); // Wrapper Float to string Float floatWrapper = new Float(999.999); String floatWrapperStr = String.valueOf(floatWrapper); System.out.println("floatWrapperStr : " + floatWrapperStr); Float floatWrappeDecimals = new Float(2343.343); String floatWrappeDecimalsStr = String.valueOf(floatWrappeDecimals); System.out.println("floatWrappeDecimals : " + floatWrappeDecimalsStr); } }
Output:
primitiveFloatStr :900.0 piStringValue : 3.14 floatWrapperStr : 999.999 floatWrappeDecimals : 2343.343
Both methods take float primitive values but passed wrapper Float object in the examples which are converted from Float to primitive float. This is called unboxing.
Read more on What is Boxing and Unboxing in java
4. Conclusion
In this article, We've seen the example programs to convert wrapper or primitive float values to String. There are two ways to do with API methods and those are String.valueOf(float) and Float.toStrig(). Here most of the developers use the String valueOf() method but this method internally calls Float.toString() method. So, It is good to use Float.toString() in real-time applications.
GitHub Code
How to covert String to Int in java?
No comments:
Post a Comment
Please do not add any spam links in the comments section.