-4

I'm getting the distance in string like "8.9km". I need to convert it to float.

So if the string is "8.9km", the corresponding float will be 8.9

How to achieve this in Java?

Note: The value can change to miles/meters also, since it is retrieved from Google Maps

webgenius
  • 856
  • 3
  • 13
  • 30

7 Answers7

3

You can try the following code,

String dis = "8.9km";
Float distance = Float.valueof(dis.substring(0,dis.indexOf("km")));
float d = distance.floatValue();
Aditya
  • 1,214
  • 7
  • 19
  • 29
1

You can do

String distance = "8.9km";
float distanceInFloat = Float.parseFloat(distance.substring(0,distance.indexOf("km")));
System.out.println(distanceInFloat);

Output :

8.9
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Please create a float variable to make the example more clear – Vitaly Olegovitch Aug 02 '14 at 09:40
  • But this will parse a float even if 'km' is in the middle of the string e.g. "1.5km Some Text" will return 1.5 with your method, despite it clearly being invalid input. – bcsb1001 Aug 02 '14 at 09:46
  • 1
    @bcsb1001 OP has clearly stated `I'm getting the distance in string like "8.9km".` and I have answered based on that. Question says nothing otherwise. Validation if any should be taken care separately. – Aniket Thakur Aug 02 '14 at 09:48
1

Use following code:

    String s="8.9km";
    s=s.replace("km", "");
    float f=Float.parseFloat(s);
    System.out.println(f);

Output:

8.9

Hope it helps.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
1

try the following for removing all non-digits(excluding '.') from a string

String distance = "7.9km";
float floatValue = Float.parseFloat(distance.replaceAll("[^\\d.]",""));
System.out.println(floatValue);
pratZ
  • 3,078
  • 2
  • 20
  • 29
1
String str = "8.9km";
str = (str.replace("km", ""); //remove the km
float f = Float.parseFloat(str); //convert the string to a float

The float f will now have a value of 8.9

System.out.println(f);

Output:

8.9
Isaac Adni
  • 831
  • 4
  • 14
  • 29
  • The value can change to miles/meters also, since it is retrieved from Google Maps – webgenius Aug 02 '14 at 09:52
  • 1
    @webgenius you can use try/catch `try { str = (str.replace("km", ""); str = (str.replace("miles", ""); str = (str.replace("meters", ""); } catch (Exception e) { }` Then you will have your string without the km, miles or meters, as although it will not be able to do two of the three statements, it will be able to do the other one, and the try/catch stops the other two from causing errors. – Isaac Adni Aug 02 '14 at 10:35
1

Try this:

public float parse(String string, String suffix) { // suffix can be m, km etc.
    if (!string.endsWith(suffix)) {
        throw new IllegalArgumentException();
    }
    string = string.subString(0, string.length() - suffix.length()); // get rid of the suffix we have at the end
    return Float.parseFloat(string); // parse the float from what we have left
}

If the float is invalid, or the string doesn't end in "km", a NumberFormatException or IllegalArgumentException will be thrown, respectively.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
1

Try this.. i hope it will help

String km = "8.9km";
float kmInFloat=Float.parseFloat(km.replaceAll("km",""));
System.out.println(kmInFloat);

Output : 8.9

one
  • 108
  • 5