2014-12-06 105 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() 
相關問題