2013-04-25 111 views
0

我使用如何将%02d:%02d:%02d转换为double?

Min_txtvw.setText(String.format(mTimeFormat, hour, min, sec)); 

现在 average=distance*60*60/seconds //我的函数为秒发现平均 我不得不从Min_txtvw 我做它像之下,但它给NumberformatException得到的时间,

设置时间上 TextView
try { 

      String s = Min_txtvw.getText().toString(); 
      double d = Double.valueOf(s.trim()).doubleValue(); 
      System.out.println("average distance is"+d); 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

我如何转换TextView值并将其转换为双精度值?

回答

0

您得到NumberformatException,因为您的变量S包含“:”字符串值,您需要使用replaceAll()方法替换这些值。

尝试以下方法,

try { 
     String s = Min_txtvw.getText().toString(); 
     s.replaceAll (":",""); 
     double d = Double.valueOf(s.trim()).doubleValue(); 
     System.out.println("average distance is"+d); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
+0

此行'一个String = Min_txtvw.getText()的toString()之后;''放的System.out.println(S);',并告诉我它显示 – Lucifer 2013-04-25 06:28:29

+0

04-25 12:02:25.841:I/System.out(12690):s的值是00:01:18 04-25 12:02:25.841:I/System.out(12690):formatted value of s是00:01:18 – Raj 2013-04-25 06:35:45

+0

@Raj,我已经更新了答案,请再试一次。 – Lucifer 2013-04-25 06:37:46