Can i convert String to float coming from server? [duplicate] - java

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 .

Related

Why my function is displaying a weird result when converting string to double? [duplicate]

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));

ParseFloat String Java [duplicate]

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

Error while converting the String to float.(NumberFormatException) [duplicate]

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);

How to change a float into a String in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 7 years ago.
Is it possible to change a float value to a String? If possible, is it also possible to converting it to a String while rounding the number to the nearest integer?
For example if I have a float such as 2.335 then, can I change it to a String of value "2.335" or "2" (by rounding it)?
Use java Float class:
String s = Float.toString(25.0f);
if you want to round down a number, simply use the Math.floor() function.
float f = 2.9999f;
String s = Float.toString(Math.floor(f));//rounds the number to 2 and converts to String
first line rounds the number down to the nearest integer and the second line converts it to a string.
Another way of doing this is using the String.valueOf(floatNumber);
float amount=100.00f;
String strAmount=String.valueOf(amount);
To do this you can simply do
float example = 2.335
String s = String.valueOf(Math.round(example));
To convert a float to a String:
String s = Float.toString(2.335f);
Rounding can be done via
String.format("%.5g%n", 0.912385);
which returns 0.91239
For a more elaborate answer, see Round a number in Java
Your first requirement can be fullfilled with String.valueOf
float f = 1.6f;
String str = String.valueOf(f);
For roundoff you can use Math.round and not Math.floor. As Math.floor will convert 1.6 to 1.0 and not 2.0 while Math.round will roundoff your number to nearest integer.

Convert a string to a float in java [duplicate]

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");

Categories