This question already has answers here:
Convert float to String and String to float in Java
(10 answers)
Closed 6 years ago.
Can you tell me how to achieve this in android:
Transform a String into a float as in:
String temp="1,000,00"
to
float f=100f
I had already gone through links. Getting this exception :
java.lang.NumberFormatException: Invalid float: ""
Have you tried like this:
String s = "100";
float f = Float.parseFloat(s);
and make sure that the String you are parsing is indeed a float and is not null or empty
You should be using the Float class's parseFloat method.
It sounds like the problem is with the actual string you are using though - can you post that?
Use the Float class.
// String to Float
float temp = Float.parseFloat("100f");
// Float to String:
String str = Float.toString(100.0f);
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.
For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!
public static String convertMultiply(String number, String conversion) {
number=number.replace(",", ".");
BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
res=res.stripTrailingZeros();
return res.toPlainString().replace(".", ",");
}
thanks in advance!
Double is an approximation of decimal values in Java. Instead, replace your line using double with:
BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));
This question already has answers here:
Best way to parseDouble with comma as decimal separator?
(10 answers)
Closed 4 years ago.
I have this String -> "100,24" and I want join in the var Java type float
But when I have the parse, I get error.
article.cost((Float.parseFloat(array.get(y))));
I need help, ty.
The number format must contain dot(.) instead of comma(,). That's why you get the exception. However, you can also use parseFloat with String's replace method to convert float by using comma.
class NumberTest {
public static void main(String[] args) {
String y = "100,24";
float num = Float.parseFloat(y.replace(',','.'));
System.out.println(num);
}
}
Your issue is the comma, "100,24", that string is not a valid float. So an error will be thrown if you try converting it to one. However if this is what you intended "100.24" that should fix your problem, because that is a valid float type.
So what you do is replace the ',' with a '.' and then try converting. i.e.
String floatStr = "100,24".replace(",", "."); // Change to a correct float value
float newFloat = Float.parse(floatStr); // parse and get your new float
This question already has answers here:
Convert float to String and String to float in Java
(10 answers)
Closed 7 years ago.
I want to change a float coming from server to string or string to float.
Can i convert string to float coming from server?
String coming;
coming = new String(rec.getData());
Toast.makeText(getApplicationContext(),comnig,Toast.LENGTH_LONG).show();
Yes you can convert a String to a float.
Simply do:
Float.parseFloat(string);
You may want to surround this with a try and catch because if the string is not compatible with type float it will create a stack-trace.
check this out
https://stackoverflow.com/a/7552675/2329972
converting string to float
float f = Float.parseFloat(coming);
it's better to use try -catch , because your coming value may contain something that are not compatible with float type .
This question already has answers here:
Formatting a String to Remove Scientific Notation - Java
(4 answers)
Closed 9 years ago.
I'm trying to convert a HEX string into float from a data that comes from a device:
The device output in the LCD display,
0x00ac and the corresponding float value is 5.06
The method that calculated the value is:
final byte[] temp = new byte[1];
temp[0] = ba[0];
float fff = hexToFloat(bytesToHex(temp)).floatValue();
final float ff = ( fff / 42) * 1000;
String floatString = Float.toString(ff);
However the floatString output string contains "E-" notation. I need to remove this. Also it seems that the value of ff is slightly different from what the device output in the LCD.
I don't see how 0x00ac can be equal to 5.06, but here is how to get rid of the scientific notation with BigDecimals:
BigDecimal num = new BigDecimal(fltInput);
String numWithNoExponents = num.toPlainString()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java : convert float to String and String to float
I'm extracting some numbers from a string, these are being stored as another string. Is there a way to convert these strings into a float?
i tried float f = "string"; but this didnt work.
Thanks
You're looking for Float.parseFloat().
Try Float.parseFloat but it does throw a RuntimeException if it fails so this is one time I would recommend catching it.
try {
Float.parseFloat("0.4")
} catch (NumberFormatException e){
//input is not a float
}
If you want more precision then look at Double.parseDouble() or even the BigDecimal string constructor
This way---: Float.parseFloat("0.4");