2014-12-06 104 views
0

我不能将double转换为字符串而不会失去精度。将Double转换为String。丢失精度

Double latitude = gps.getLatitude(); 
String latitude1 = Double.toString(latitude); 
new onbuttonclickHttpPost().execute(latitude1); 

这里是异步类休息。而在文章中,我送字符串,它是3.0这是不能接受

public class onbuttonclickHttpPost extends AsyncTask<String, Void, Void> { 

    @Override 
    protected Void doInBackground(String... f_url) {  

     byte[] result = null; 
     String str = ""; 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("_HERE_GO_MY_URL"); 

     try { 

       // Add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("lng", f_url[0])); 
       nameValuePairs.add(new BasicNameValuePair("lat", "X")); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 
       StatusLine statusLine = response.getStatusLine(); 
       if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){ 
       result = EntityUtils.toByteArray(response.getEntity()); 
       str = new String(result, "UTF-8"); 
      } 
      } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     return null; 
    } 

    /** 
    * on getting result 
    */ 
    protected void onPostExecute(String result) { 
     // something with data retrieved from server in doInBackground 
    } 
} 

字符串看起来像这样3.0而应3.518972061574459

如何与全精度转换?

编辑:补充一点的代码

+2

双任何具体的原因是不精确的,但并不准确。它的可能是纬度被截断之前的第一次作业...发布您的完整代码 – Reimeus 2014-12-06 13:38:54

+0

http://stackoverflow.com/questions/20353319/java-double-to-string-with-specific-precision 退房十进制格式,只是快速搜索 快乐编码 – RedKlouds 2014-12-06 13:42:15

+0

这怎么会发生?你确定你的双倍是3.518972061574459。可能是你可以发布更多的代码 – Jerome 2014-12-06 13:43:55

回答

0

你这样做是正确的(你可以看到这里,我得到的所有的小数:http://goo.gl/pORXg2)的方式。也许问题在于你如何打印出来。

+0

所以..我怎么可以传递完整的dobule POST数据在nameValuePair – pr0metheus 2014-12-06 14:00:01

0

尝试双转换为字符串明确与格式

String output=String.format("%.10f", 3.518972061574459); 

,你会在输出了“3.5189720615”。

0

是否有您不使用

Double latitude = gps.getLatitude(); 
String latitude1 = latitude==null?"":latitude.toString() 
相关问题