将双精度转换为字符串 [英] Converting double to string

查看:35
本文介绍了将双精度转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码:

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

是我做错了什么还是我在这里遗漏了一步.

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

我在尝试转换时遇到错误 NumberFormatException.

I get error NumberFormatException when trying to convert this.

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;
  }         
});

我正在尝试在 onTouchListener 中执行此操作.我会发布更多代码,基本上当用户触摸编辑文本框时,我希望信息计算填充编辑文本框.

I am trying to do this in an onTouchListener. Ill post more code, basically when the user touches the edittext box i want the information to calculate a fill the edittext box.

推荐答案

double total = 44;
String total2 = String.valueOf(total);

这会将双精度转换为字符串

This will convert double to String

这篇关于将双精度转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆