Convert String to Double in Java

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:

  1. In solution 1 used replace method to replace all , i.e. replace(",", "");
  2. In solution 2 used NumberFormat. This class provides the interface for formatting and parsing numbers.
  3. In solution 3 removed all , from testString 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 doubleyourself 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



Leave a reply



Submit