Converting String to Double in Android

前端 未结 8 656
感情败类
感情败类 2020-12-09 02:29

Trying to get double values from an EditText and manipulate them before passing them to another Intent. Not using primitive data type so I can use toString methods.

相关标签:
8条回答
  • 2020-12-09 02:47

    You seem to assign Double object into native double value field. Does that really compile?

    Double.valueOf() creates a Double object so .doubleValue() should not be necessary.

    If you want native double field, you need to define the field as double and then use .doubleValue()

    0 讨论(0)
  • 2020-12-09 03:00

    I would do it this way:

    try {
      txtProt = (EditText) findViewById(R.id.Protein); // Same
      p = txtProt.getText().toString(); // Same
      protein = Double.parseDouble(p); // Make use of autoboxing.  It's also easier to read.
    } catch (NumberFormatException e) {
      // p did not contain a valid double
    }
    

    EDIT: "the program force closes immediately without leaving any info in the logcat"

    I don't know bout not leaving information in the logcat output, but a force-close generally means there's an uncaught exception - like a NumberFormatException.

    0 讨论(0)
  • 2020-12-09 03:00

    I had the same issue, but I have just figured out that :

    • parsing the EditText value in the Oncreate method caused the app to crash because when the app starts, there are no values to parse or maybe the placeholders which are letter.

    My code:

    package com.example.herodav.volumeapp;
    
    import android.renderscript.Double2;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.*;
    import android.widget.*;
    
    import org.w3c.dom.Text;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText height, length, depth;
        TextView volume;
        double h,l,d,vol;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            height = (EditText)findViewById(R.id.h);
            length = (EditText)findViewById(R.id.l);
            depth = (EditText)findViewById(R.id.d);
            volume = (TextView)findViewById(R.id.v);
    
            Button btn = (Button)findViewById(R.id.btn);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    calculateVolume();
                    volume.setText("Volume = " + String.valueOf(vol));
                }
            });
        }
    
        public void calculateVolume(){
            h = Double.parseDouble(height.getText().toString());
            l = Double.parseDouble(length.getText().toString());
            d = Double.parseDouble(depth.getText().toString());
            vol = h*l*d;
        }
    }
    

    I

    0 讨论(0)
  • 2020-12-09 03:09

    What about using the Double(String) constructor? So,

    protein = new Double(p);
    

    Don't know why it would be different, but might be worth a shot.

    0 讨论(0)
  • 2020-12-09 03:09
      kw=(EditText)findViewById(R.id.kw);
        btn=(Button)findViewById(R.id.btn);
        cost=(TextView )findViewById(R.id.cost);
    
    
                btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { cst =  Double.valueOf(kw.getText().toString());
                cst = cst*0.551;
                cost.setText(cst.toString());
            }
        });
    
    0 讨论(0)
  • 2020-12-09 03:11

    i have a similar problem. for correct formatting EditText text content to double value i use this code:

    try {
            String eAm = etAmount.getText().toString();
            DecimalFormat dF = new DecimalFormat("0.00");
            Number num = dF.parse(eAm);
            mPayContext.amount = num.doubleValue();
        } catch (Exception e) {
            mPayContext.amount = 0.0d;
        }
    

    this is independet from current phone locale and return correct double value.

    hope it's help;

    0 讨论(0)
提交回复
热议问题