Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - 将double转换为字符串(Converting double to string)

I am not sure it is me or what but I am having a problem converting a double to string.

(我不确定它是我还是什么,但我在将double转换为字符串时遇到问题。)

here is my code:

(这是我的代码:)

double total = 44;
String total2 = Double.toString(total);

Am i doing something wrong or am i missing a step here.

(我做错了什么,或者我错过了一步。)

I get error NumberFormatException when trying to convert this.

(尝试转换它时,我收到错误NumberFormatException 。)

totalCost.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
    try {
      double priceG = Double.parseDouble(priceGal.getText().toString());
      double valG = Double.parseDouble(volGal.toString());
      double total = priceG * valG;
      String tot = new Double(total).toString();
      totalCost.setText(tot);
    } catch(Exception e) {
      Log.e("text", e.toString());
    }

    return false;
  }         
});

I am trying to do this in an onTouchListener.

(我试图在onTouchListener中执行此操作。)

Ill post more code, basically when the user touches the edittext box i want the information to calculate a fill the edittext box.

(我发布更多代码,基本上当用户触摸edittext框时,我希望信息计算填充edittext框。)

  ask by Brandon Wilson translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
double total = 44;
String total2 = String.valueOf(total);

This will convert double to String

(这会将double转换为String)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...