Java 如何在android中将double转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16334820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:22:33  来源:igfitidea点击:

how to convert double to string in android

javaandroidinflate

提问by Aoyama Nanami

i want to convert string in my constructor into double, i can't change it in my constructor, because in one process, the variable must in double, and other class must be string, can you tell me the way to convert it..? this is my constructor :

我想在我的构造函数中将字符串转换为double,我不能在我的构造函数中更改它,因为在一个过程中,变量必须是double,而其他类必须是string,你能告诉我转换的方法吗.. ? 这是我的构造函数:

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 base adapter, i want to change getInuNilai() into string this is my base adapter :

在我的基本适配器中,我想将 getInuNilai() 更改为字符串,这是我的基本适配器:

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 setText getInuNilai() because its return into double, can you tell me how to convert it?

我不能 setText getInuNilai() 因为它返回为 double,你能告诉我如何转换它吗?

采纳答案by Opiatefuchs

      String yourDoubleString = String.valueOf(yourDouble);

if You want to have the returned double from Your getInuNilai() Method as a String:

如果您想将 getInuNilai() 方法返回的 double 作为字符串:

first get Your double from this Method:

首先从这个方法中得到你的双倍:

    double inuNilaiDouble = getInuNilai(); 

and parse it into String:

并将其解析为字符串:

     String inuNilaiString = String.valueOf(inuNilaiDouble);

or

或者

     String inuNilaiString = Double.toString(inuNilaiDouble);

if you want this outside Your DataItem.java, make a reference of DataItem and get the double:

如果您想在您的 DataItem.java 之外使用它,请引用 DataItem 并获得双精度值:

    double inuNilaiDouble = mReferenceOfDataItem.getInuNilai(); 

and then parse it into String like shown above.

然后将其解析为字符串,如上所示。

回答by Aleks G

One way of doing it would be:

一种方法是:

double x = 123.45;
String res = "" + x;

There are other ones, e.g. String res = new String(x);- pick the one you like.

还有其他的,例如String res = new String(x);- 选择你喜欢的那个。

回答by Jainendra

Try this:

尝试这个:

String s = Double.toString(yourDouble);

回答by Michael 'Maik' Ardan

You may use:

您可以使用:

String yourStringVariable = Double.toString(yourDoubleVariable/value);

There is also a concept that if you concat your primitive datatype to a string value, makes it a String.

还有一个概念,如果您将原始数据类型连接到字符串值,则使其成为字符串。

String yourStringVariable = yourDoubleVariable + "";

Edit:

编辑:

Your method getInuNilai()returns a double value. You may use the following code:

您的方法getInuNilai()返回双精度值。您可以使用以下代码:

String yourStringVariable = yourDataItemVariable.getInuNilai() + "";

OR:

或者:

String yourStringVariable = Double.toString(yourDataItemVariable.getInuNilai());

回答by URAndroid

use this to convert from Double to String Double.toString(yourDouble);

使用它从 Double 转换为 String Double.toString(yourDouble);