How could I convert from float to string or string to float?
In my case I need to make the assertion between 2 values string (value that I have got from table) and float value that I have calculated.
String valueFromTable = "25";
Float valueCalculated =25.0;
I tried from float to string:
String sSelectivityRate = String.valueOf(valueCalculated );
but the assertion fails
You can try this sample of code:
found here
well this method is not a good one, but easy and not suggested. Maybe i should say this is the least effective method and the worse coding practice but, fun to use,
the empty quotes, add a null string to the variable str, upcasting 'val' to the string type.
Float to string - String.valueOf()
String to Float - Float.parseFloat()
This is a possible answer, this will also give the precise data, just need to change the decimal point in the required form.
Output will be
Using Java’s
Float
class.To compare it's always better to convert the string to float and compare as two floats. This is because for one float number there are multiple string representations, which are different when compared as strings (e.g. "25" != "25.0" != "25.00" etc.)