Convert a string to a float in java [duplicate] - java

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

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

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

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 .

Getting Rid of Decimal place in Java? [duplicate]

This question already has answers here:
How to Replace dot (.) in a string in Java
(4 answers)
Closed 8 years ago.
I am reading in java inputs in format below
2499.873639
32.374242
0.610059
...
Now i want to get rid of the decimal place and have them in this format
2499873639
32374242
610059
...
I have this code which does it for smaller number for not for larger numbers. The Larger numbers become negative (i think this overflowing) and giving it junk values.
BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("1000000.")
If i increase the 0's by another two
BigDecimal b = new BigDecimal(a).multiply(new BigDecimal("100000000.")
It works for larger numbers but not smaller numbers. In short of having a bunch of if's is there anyway to fix this issue?
Use this :
BigDecimal b = new BigDecimal(a.toString().replace('.', ''));
String formattedInput = (String.valueOf(input)).replace(".", "");
You can do this with String replacement functions.
public static BigDecimal noDecimal(BigDecimal b) {
return new BigDecimal(b.toPlainString().replace(".", ""));
}
If you already have a String rather than a BigDecimal, this can be simplified to this:
public static BigDecimal noDecimal(String s) {
return new BigDecimal(s.replace(".", ""));
}
Convert the double to String, if it is not.
Then use String.replace('.', '');
Then Convert back to int or long, if necessary.

Categories