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

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.

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

How to convert component of String[] or Object[] to other type [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Java - Convert integer to string [duplicate]
(6 answers)
Closed 8 years ago.
I have knowledge of wrapper class methods, but I want to know how to convert the component of String[] or Object[] to other type like int,float or Date format. for example
BufferedReader br = new BufferedReader(new FileReader(csvfile));
String curr;
while((scurr=br.readLine())!null) {
String[] input = scurr.split(",");
}
Now I want assign the component of String to primitive type(assume that my input string contains integer value)
but when I am trying to do
int i = input[0]
I am getting following suggestions:
1. change the type of i to String or
2. change the type of input to int
Is there any way to tackle the above scenario
edit:
I am really sorry guys, I really don't want ask duplicate questions, but after going through your answers and analyzing my scenario, I understood my mistake. So I would like to delete this post. How to do that without impacting the community please guild me
You can use Integer.parseInt() without an issue.
int i = input[0] // i is an int while input[0] is a String
Now
int i=Integer.parseInt(input[0])
will convert String to int
Integer.parseInt()

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.

How can I convert string to double? [duplicate]

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

Categories

Resources