在Google地图方向上将字符串距离转换为double

时间:2016-03-18 08:14:35

标签: java android google-maps-android-api-2

您好我想询问是否可能通过给出谷歌地图指示转换字符串距离,我会将其转换为小数点?

    DecimalFormat df = new DecimalFormat("#.##");
    Log.e("DISTANCEHECKER", "" + df.format("29 m"));
29将改变它取决于谷歌方向的结果可能它将改变为Km或M它取决于。我需要得到精确的十进制数。

1 个答案:

答案 0 :(得分:2)

试一试。它是一个有效的准则。将字符串距离格式化为任何类型的单位.....

 try{
        String distance = "29.336 m"; //Notice that one space between digit and unit.
        String arr_distance = distance.split(" "); //split on space
        Double digit_distance =Double.parseDouble(arr_distance [0]); // First element as a double to digit.
        String unit_distance = arr_distance[1]; // Second Element as String to unit
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println(digit_distance);
        switch(unit_distance){
              case "m":
                  System.out.println("DISTANCEHECKER" + df.format(digit_distance));
                  break;
              case "km":   
                  System.out.println("DISTANCEHECKER" + df.format(digit_distance*1000));
                  break;
             ............
             ................  //more Code Omitted.as per your requirement
     }
     }catch(Exception e){
            System.out.println(e.getMessage());
        }