How can I convert string to double? [duplicate] - java

This question already has answers here:
Converting String to Double in Android
(9 answers)
Closed 3 years ago.
I am developing an android app. I need to convert string to double. How can I do it?

You just need this :
double d=Double.parseDouble(myString);
If your String value cannot be parsed to double it will throw NumberFormatException. So better you put the parseDouble statement inside try catch block.

here is the example
try{
double dbl = Double.parseDouble("99.882039");
catch(NumberFormatException ex){
// handle exception
}

double d = Double.parseDouble(theString);

That's not a thing of android but if you're sure you string can be expressed as a double you can do it like this with simple Java:
Double.valueOf("1.2");

Related

Java double calculate and convert to String [duplicate]

This question already has answers here:
Converting double to string
(17 answers)
Closed 1 year ago.
so I got a Problem coding in Java.
I have some doubles declared and I need to calculate with them. But I dont know who I convert the doubles to a result that can be posted in a JLabel as a String. It always says something like: cannot convert double to String. I have tried toString method but I am either doing it wrong or it just doesnt work. Any ideas for a working code?
You can use String#valueOf.
Demo:
public class Main {
public static void main(String[] args) {
double val = 12.34;
String strVal = String.valueOf(val);
System.out.println(strVal);
}
}
Output:
12.34

How to check whether the string is able to convert to float or int [duplicate]

This question already has answers here:
How to check if a String is numeric in Java
(41 answers)
Closed 6 years ago.
I would like to check whether the string is able to convert to float or int.
For example:
I received
temp = 36.50
This value can be converted into float using
float Temp = Float.parseFloat(temp);
But what if I received
temp = 36.#0
My app will crash. So how can I check whether the string I received is able to convert to float?
Also for Int how do I do that?
try this
float temp = 0 ;
try {
temp = Float.parseFloat(temp);
} catch (NumberFormatException ex) {
// Not a float
}
/*
you can do something with temp variables in here
*/
You can determine if String is Integer or not then convert it to avoid crashes or try-catch.
Please check this link to learn how
Determine if a String is an Integer in Java

How to convert JOptionPane String Input into an Int? [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
I am beginning to learn Java and I am stuck on how to receive the user's input as a String and converting it to an int afterwards so I can use If Statements. Thank you for reading guys, good programming for all.
Try this:
JOptionPane pane // your control
int result = Integer.parseInt(pane.getInputValue().toString());
System.out.println("result = " + result);

Extract a double from a string[] in Java? [duplicate]

This question already has answers here:
Parsing a currency String in java
(2 answers)
Closed 6 years ago.
Note: Java
I have a String[] called moneyEntered.
moneyEntered[0] has the value "$5.00"
I wish to create a new double (called amountPendingDeduction) which extracts the value "5.00" from moneyEntered[0] (i.e. removing the dollar sign and making the rest a double).
How do I do this?
If you are working with currency, double is just fine:
String money = moneyEntered[0].substring(1);
double moneyValue = Double.parseDouble(money);
Try:
Double amountPendingDeduction = Double.parseDouble(moneyEntered[0].replace("\\$", ""));
Since String is immutable, nothing will happen with the dollar sign in the moneyEntered[0] string.

Convert double into String 12Digit [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 8 years ago.
I am converting double number into string but i am not getting as it is.
following is my code and result:
double d = 123456789123.0;
String test = String.valueOf(d);
System.out.println("InString"+test);
Result:
InString :1.23456789123E11
expected result: 123456789123.0
Try this:
NumberFormat nf = new DecimalFormat("000000000000.0");
String test = nf.format(d);
Consider NumberFormat class from standard java library.
Use it's methods to format a double value.

Categories

Resources