String to double ("a + b")

string to double android
convert string to double c#
string to double swift
string to double matlab
string to double kotlin
convert string to double python
convert string to double vba
java convert string to double with 2 decimal places
public class simpleClass{
    public static void main(String[] args){
        String a = "2 + 3";
        double b = Double.parseDouble(a);

That's just simple code to show what's my problem.

Why this won't work when I run the program?

I'm making a simple calculator in Spring and I'm adding numbers as a String (also +, -, /, *) but after parsing I'm getting errors in IJ.

You can use ScriptEngine to evaluate a string and parse it to a double.

Like this:

public static void main(String[] args) {
    // Base Query
    String a = "5 * 2";

    // Query Result
    double b = EvalMath(a);

    // Print Result

// Evalute Math in String
static double EvalMath(String a) {
    double result = 0;

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    try {
        result = Double.parseDouble(engine.eval(a).toString());
    } catch (ScriptException e) {
        // TODO Auto-generated catch block

    return result;

Hope it helps! :D

Java Convert String to Double examples, Given a String, the task is to convert it into Double in Java. Examples: Input: String = "20.156" Output: 20.156 Input: String = "456.21" Output: 456.21. We can use  Converts a valid String to a double. The input string should start with a digit. If the string contains non-digit characters, the function will stop performing the conversion. For example, the strings "123.45 ", "123 ", and "123fish " are converted to 123.45, 123.00, and 123.00 respectively.

Double.parseDouble() can't translate characters that are doubles into doubles, so ' + ' will break that line.


string a1 = "2";
string a2 = "3";
double b = Double.parseDouble(a1) + Double.parseDouble(a2);

Java Convert String to double, You can use Double.parseDouble() to convert a String to a double : String text = "​12.34"; // example String double value = Double. It is generally used if we have to perform mathematical operations on the string that contains double number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is double, we need to convert string to double. To do so, we use Double.parseDouble() method.

Check it First: Double.parseDouble() which takes string. If it's not a number it'll throw an NumberFormatException. 2 + 3 is a expression. So this method can't parse it to double.

Full Solution here:

public class simpleClass {
public static void main(String[] args) {
    String a = "2 + 3";
    /// First of all don't add space on equation
    a.replaceAll(" ", ""); // This will remove all whitespace
    double op1 = 0, op2 = 0; // variable for storing value
    char operand = '\0'; // operand
    boolean flag = false; // false means no value taken for operation

    for (int i = 0; i < a.length(); i++) {
        String tmp = "";
        if (Character.isDigit(a.charAt(i))) {
            tmp += a.charAt(i);
            if (!flag)
                op1 = Double.parseDouble(tmp);
                op2 = Double.parseDouble(tmp);
        } else if (a.charAt(i) == '+' || a.charAt(i) == '-' || a.charAt(i) == '*' || a.charAt(i) == '/') {
            operand = a.charAt(i);
            flag = true; // now we have a value in op1
    double result = 0;

    if (operand == '+') {
        result = op1 + op2;
    } else if (operand == '-') {
        result = op1 - op2;
    } else if (operand == '*') {
        result = op1 * op2;
    } else if (operand == '/') {
        result = op1 / op2;



Output: 5

Convert String to Double in Java, It is generally used if we have to perform mathematical operations on the string that contains double number. Whenever we get data from textfield or textarea,  @TinyBelly: you need to extract the part of the text from the string that you want to parse for the double. Here's one way you could do it for your case, though I can't guarantee it will work for everything: double total = Double.parseDouble(jlbTotal.getText().replaceAll("[^0-9.]", "")); - this basically replaces all characters that aren't a number or . to nothing, leaving only the number and

Convert String to double in Java, Similarly, we can convert a String into a boxed Double using the Double.valueOf method: ? 1. assertEquals( 1.23 , Double  X = str2double(str) converts the text in str to double precision values. str contains text that represents real or complex numeric values. str can be a character vector, a cell array of character vectors, or a string array. If str is a character vector or string scalar, then X is a numeric scalar.

Java Convert String to double, Double.valueOf(). Example to convert a String 3.142 to an Double object. String str = "3.142  Converts the value of the specified object to an double-precision floating-point number, using the specified culture-specific formatting information. ToDouble (UInt64) Converts the value of the specified 64-bit unsigned integer to an equivalent double-precision floating-point number. ToDouble (UInt32) Converts the value of the specified 32-bit

Converting Java String to Double, double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0);. Convert string to double. Parses str interpreting its content as a  Since C++11 converting string to floating-point values (like double) is available with functions: stof - convert str to a float stod - convert str to a double stold - convert str to a long double