将Double转换为String。失去精确度

时间:2014-12-06 13:33:35

标签: java android string double

我不能在不丢失精度的情况下将double转换为字符串。

查找

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

这是带休息的异步类。在POST中我发送字符串,它是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

如何以全精度转换?

编辑:添加更多代码

3 个答案:

答案 0 :(得分:0)

你正确行事的方式(你可以看到我得到了所有小数:http://goo.gl/pORXg2)。也许问题是你如何打印它。

答案 1 :(得分:0)

尝试使用格式

显式地将double转换为String
String output=String.format("%.10f", 3.518972061574459);

你将在输出中得到“3.5189720615”。

答案 2 :(得分:0)

您是否有任何特定原因未使用

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