How to Convert String value into Double or Int value in Java?
As others have said in the comments, NumberFormatException is happening because you are trying to parseDouble without removing the $ from the number.
In this case, you can use substring() to get everything after the first char:
String one = "$615.00";
String two = "$15.00";
double newone = Double.parseDouble( one.substring(1) );
double newtwo = Double.parseDouble( two.substring(1) );
System.out.println(newone-newtwo);
Results in 600.00
Convert String to Double - JAVA
To me it seems like you are selecting a value from a combobox then trying to add them together to create a grand total, with the total being updated every time you select a new value from the combobox.
This seems to be working for me, Note I've taken the first line away because it interfered with the existing total.
tot= Double.parseDouble(txtTot.getText());
CMB = Double.parseDouble((String) jComboBox1.getSelectedItem());
Total2=tot+CMB;
txtTot.setText(Double.toString(Total2));
Exception while trying to convert String value to Double in Java
Solution 1:
String testString = "1,514,230.15".replace(",", "");
double d = Double.parseDouble(testString);
System.out.println(d);
Or Solution 2:
String input = "1,514,230.15";
NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
double result = numberFormat.parse(input).doubleValue();
System.out.println(result);
Or Solution 3:
String testString = "1514230.15";
double d= Double.parseDouble(testString);
System.out.println(d);
Output:
1514230.15
Explanation:
- In
solution 1
usedreplace
method to replace all,
i.e.replace(",", "");
- In
solution 2
used NumberFormat. This class provides the interface for formatting and parsing numbers. - In
solution 3
removed all,
fromtestString
i.e.String testString = "1514230.15";
Converting String to Double for calculation cause me problem with decimal numbers, how to round it?
You are losing precision when you make calculation with double
, to make calculation, it better to use BigDecimal
, so instead I would go :
String str = "21.90";
BigDecimal result = new BigDecimal(str).subtract(BigDecimal.valueOf(21.8));
System.out.println(result);
=> 0.10
Convert string to double without scientific notation
The methods you have used do not return 1.08595E8
, instead, they return the number and what you are complaining about is the representation of that number in the console (or as a String
).
However, you can specify how to output a double
yourself with a specified formatting, see this example:
public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
double: 1.085950005E8
engineering: 108595000.5
plain: 108595000.5
rounded with fix decimal places: 108595000.50
Related Topics
Jcomponents Not Showing Up With Picture Background
How Does a Java Hashmap Handle Different Objects With the Same Hash Code
"Missing Return Statement" Within If/For/While
Junit Test For System.Out.Println()
How to Set Time Zone of a Java.Util.Date
Extends Jframe Vs. Creating It Inside the Program
Initialize Class Fields in Constructor or At Declaration
Java Hashmap: How to Get Key from Value
How to Increase the Java Stack Size
What Is an Initialization Block
How to Add Jars to Maven 2 Build Classpath Without Installing Them
How to Address Unchecked Cast Warnings
How to Unescape a Java String Literal in Java
Convert a Json String to Object in Java Me
Java: Recommended Solution For Deep Cloning/Copying an Instance