float as well as double are 2 information type which is used to shop floating betoken values inwards Java as well as nosotros oft ask to convert String to float inwards Java as well as sometimes fifty-fifty a Float object or float primitive to String. One thing, which is worth remembering most floating betoken numbers inwards Java is that they are gauge values, a float value 100.1f may concur actual value equally 100.099998, which volition live on clear when nosotros direct maintain seen examples of converting float to String as well as vice-versa. By the way, It's slow to parse String to float as well as vice-versa, equally rich Java API provides several ways of doing it. If yous already familiar alongside converting String to int or may live on String to double inwards Java, as well as hence yous tin sack extend same techniques as well as method to parse float String values. telephone commutation methods similar valueOf() as well as parseInt(), which is used to parse String to float are overloaded for most primitive information types.
If yous are especially interested on rounding of float values, yous tin sack utilisation RoundingMode as well as BigDecimal class, equally float as well as double are e'er gauge values as well as comparison 2 float variable of same values may non e'er render true, that's why it's advised, not to utilisation float for monetary calculation.
In this Java tutorial, nosotros volition kickoff meet examples of parsing String to float inwards Java as well as afterward converting float to String objects. Remember, nosotros volition utilisation float as well as Float, a wrapper class corresponding to float primitive, interchangeably because yesteryear using Java 1.5 autoboxing feature, they are automatically converted to each other, without whatever Java code.
For those, who are however inwards Java 1.4 or lower version, as well as hence tin sack utilisation Float.floatValue() to convert Float wrapper object to float primitive.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
If yous are especially interested on rounding of float values, yous tin sack utilisation RoundingMode as well as BigDecimal class, equally float as well as double are e'er gauge values as well as comparison 2 float variable of same values may non e'er render true, that's why it's advised, not to utilisation float for monetary calculation.
In this Java tutorial, nosotros volition kickoff meet examples of parsing String to float inwards Java as well as afterward converting float to String objects. Remember, nosotros volition utilisation float as well as Float, a wrapper class corresponding to float primitive, interchangeably because yesteryear using Java 1.5 autoboxing feature, they are automatically converted to each other, without whatever Java code.
For those, who are however inwards Java 1.4 or lower version, as well as hence tin sack utilisation Float.floatValue() to convert Float wrapper object to float primitive.
3 ways to parse String to float inwards Java
constructor of Float class, which accepts a String. All these methods throws NumberFormatException if float value is illegal or non parsable. For instance trying to convert a String "#200.2" volition throw Exception inwards thread "main" java.lang.NumberFormatException: For input string: "#200.2". By the means it's legal to move yesteryear suffix "f" or "F" along alongside floating betoken set out e.g. "200.2F" volition non throw this error. Similarly, yous tin sack equally good utilisation same technique to parse whatever negative floating betoken set out String to float inwards Java, minus sign (-) is permitted inwards float String. You tin sack utilisation code snippets given inwards instance section to parse String to float inwards Java. One thing, which is worth remembering, piece dealing alongside String as well as float is that, comparison them inwards String format as well as equally float values may render dissimilar result. As shown inwards next example
float f1 = 414.23f;
float f2 = Float.valueOf("414.23f");
String s1 = "414.23f";
String s2 = String.valueOf(f1);
boolean result1 = (f1 == f2);
boolean result2 = s1.equals(s2);
System.out.printf("Comparing floating betoken numbers %f as well as %f equally float"
+ " returns %b %n", f1, f2, result1);
System.out.printf("Comparing floating betoken numbers %s as well as %s equally String"
+ " returns %b %n", s1, s2, result2);
Output:
Comparing floating betoken numbers 414.230011 as well as 414.230011 equally float returns true
Comparing floating betoken numbers 414.23f as well as 414.23 equally String returns false
The reason, nosotros acquire simulated is because of "f" suffix introduce inwards String, which is non really uncommon. Also, it's practiced thought to remove whitespaces from String earlier converting them to float inwards Java.
3 instance to convert float to String inwards Java
Now nosotros know how to parse String to float inwards Java, it's fourth dimension to explore minute part, converting a float to String. This is fifty-fifty easier than kickoff part. One of the quickest means to acquire an String object from float value is to concatenate it alongside an empty String e.g. "" + float, this volition give yous String object for all your practical purpose. Apart from this dainty trick, yous equally good direct maintain distich of to a greater extent than tricks on your sleeve, yous tin sack either utilisation valueOf() method from java.lang.String class or toString() method from java.lang.Float class, both returns String object. Here is Java plan to parse String to floating betoken set out inwards Java as well as and hence convert dorsum float to java.lang.String object. String to Float Conversion Example inwards Java.
/**
*
* Java plan to parse String to float inwards Java as well as than convert float to
* String inwards Java. Remember, piece converting same value inwards float shape as well as in
* String shape volition render dissimilar result. For instance "1".equals("1.0") will
* render simulated only 1 == 1.0 may render true.
*
* @author Javin Paul
*/
public class StringToFloat {
public static void main(String args[]) {
// Parsing String to float inwards Java
// By using autoboxing Float object tin sack live on converted to float primitive
// Converting String to Float using Float.valueOf() method
String strFloat = "100.1";
float fValue = Float.valueOf(strFloat);
System.out.printf("String %s is parse to float %f inwards Java using valueOf %n"
, strFloat, fValue);
// Converting String to Float using Float.parsetFloat() method
String strFloat2 = "150.15";
float fValue2 = Float.parseFloat(strFloat2);
System.out.printf("String %s is converted to float %f inwards Java using parseFloat %n"
, strFloat2, fValue2);
// Parse String to Float Object inwards Java
String strFloat3 = "-200.2F";
Float fValue3 = new Float(strFloat3);
System.out.printf("String %s is converted to float object %f inwards Java using"
+ " Float constructor %n" , strFloat3, fValue3);
// Second role - Converting float values to String inwards Java
// Converting float information type to String inwards Java using + operator concatenation
float fValue4 = 657.2f; // recall f suffix, floating points defaults to double inwards Java
String strFloat4 = "" + fValue4;
System.out.printf("float %f is converted to String %s inwards Java using"
+ " concatenation %n" , fValue4, strFloat4);
// Parsing float to String inwards Java using Float toString method
Float fValue5 = new Float(786.86f);
String strFloat5 = fValue5.toString();
System.out.printf("Float %f is changed to String object %s inwards Java using"
+ " toString %n" , fValue5, strFloat5);
// Converting String object to float primitive inwards Java - valueOf example
float fValue6 = 919.23f;
String strFloat6 = String.valueOf(fValue6);
System.out.printf("float %f is converted to String %s yesteryear using valueOf"
+ " inwards Java %n" , fValue6, strFloat6);
}
}
Output:
String 100.1 is parse to float 100.099998 inwards Java using valueOf
String 150.15 is converted to float 150.149994 inwards Java using parseFloat
String -200.2F is converted to float object -200.199997 inwards Java using Float constructor
float 657.200012 is converted to String 657.2 inwards Java using concatenation
Float 786.859985 is changed to String object 786.86 inwards Java using toString
float 919.229980 is converted to String 919.23 yesteryear using valueOf inwards Java
That's all on this Java beginners tutorial most parsing String to float inwards Java as well as and hence converting dorsum float values to String objects. We direct maintain learned distich of useful tricks for information type conversion, which tin sack live on really handy piece working on float as well as String information types together. Just remember, both Float as well as String are immutable objects in Java as well as whatever modification on this object volition lawsuit inwards novel object.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2