Java – how to convert a double to a string in Android

I want to convert a string to double in my constructor. I can't change it in the constructor, because in a process, the variable must be double and other classes must be strings. Can you tell me the method to convert it? This is my constructor:

public class DataItem {
String ijiInvest,ijiId;
String tanggal;
double persenHmint1,inuNilai  selisih,persen_hke1;

public DataItem(String ijiInvest,double persenHmint1,Double inuNilai,double selisih,double persen_hke1,String tanggal,String ijiId) {
    // TODO Auto-generated constructor stub
    this.ijiInvest = ijiInvest;
    this.persenHmint1 = persenHmint1;
    this.inuNilai = inuNilai;
    this.selisih = selisih;
    this.ijiId = ijiId;
    this.tanggal = tanggal;
    this.persen_hke1=persen_hke1;
}

public String getIjiInvest() {
    return ijiInvest;
}

public String getIjiId() {
    return ijiId;
}

public String getTanggal() {

    return tanggal;
}

public double getPersenHmint1() {
    return persenHmint1;
}

public Double getInuNilai() {
    return inuNilai;
}

public double getSelisih() {
    return selisih;
}

public double persen_hke1(){
    return persen_hke1;
}

In my basic adapter, I want to change getinunilai() to a string. This is my basic adapter:

public class TabelAdapter extends BaseAdapter{
Context context;
ArrayList<DataItem> listData;//stack
int count;

public TabelAdapter(Context context,ArrayList<DataItem>listData){
    this.context=context;
    this.listData=listData;
    this.count=listData.size();
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return count;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position,View v,ViewGroup parent) {
    // TODO Auto-generated method stub
    View view= v;
    ViewHolder holder= null;

    if(view==null){
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view= inflater.inflate(R.layout.main,null);
        holder=new ViewHolder();
        holder.nama=(TextView)view.findViewById(R.id.name);
        holder.email=(TextView)view.findViewById(R.id.email);
        view.setTag(holder);
    }
    else{
        holder=(ViewHolder)view.getTag();

    }
    holder.nama.setText(listData.get(position).getTanggal());
//      holder.email.setText(listData.get(position).getInuNilai());
    return view;
}
static class ViewHolder{
    TextView nama,email;
}

I can't gettext getinunilai() because it returns double. Can you tell me how to convert it?

Solution

String yourDoubleString = String.valueOf(yourDouble);
String yourDoubleString = String.valueOf(yourDouble);

If you want to use the double returned by the getinunilai () method as a string:

First get your double from this method:

double inuNilaiDouble = getInuNilai();

And resolve it to string:

String inuNilaiString = String.valueOf(inuNilaiDouble);

or

String inuNilaiString = Double.toString(inuNilaiDouble);

If you want to be in dataitem To use it outside of Java, create a reference to dataitem and get double:

double inuNilaiDouble = mReferenceOfDataItem.getInuNilai();

Then parse it into string, as shown above

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>