kande
kande

Reputation: 559

Get Double from string in Java

I have a text (String) an I need to get only digits from it, i mean if i have the text:

"I'm 53.2 km away", i want to get the "53.2" (not 532 or 53 or 2)

I tried the solution in Extract digits from a string in Java. it returns me "532".

Anyone have an idea for it?

Thanx

Upvotes: 1

Views: 13475

Answers (6)

Chu Vu Hung
Chu Vu Hung

Reputation: 1

I have just made a method getDoubleFromString. I think it isn't best solution but it works good!

public static double getDoubleFromString(String source) {
        if (TextUtils.isEmpty(source)) {
            return 0;
        }

        String number = "0";
        int length = source.length();

        boolean cutNumber = false;
        for (int i = 0; i < length; i++) {
            char c = source.charAt(i);
            if (cutNumber) {
                if (Character.isDigit(c) || c == '.' || c == ',') {
                    c = (c == ',' ? '.' : c);
                    number += c;
                } else {
                    cutNumber = false;
                    break;
                }
            } else {
                if (Character.isDigit(c)) {
                    cutNumber = true;
                    number += c;
                }
            }
        }
        return Double.parseDouble(number);
    }

Upvotes: 0

Amimo Benja
Amimo Benja

Reputation: 569

The best and simple way is to use a regex expression and the replaceAll string method. E.g

String a = "2.56 Kms";

String b = a.replaceAll("\\^[0-9]+(\\.[0-9]{1,4})?$","");

Double c = Double.valueOf(b);

System.out.println(c);

Upvotes: 1

Surender Thakran
Surender Thakran

Reputation: 4054

import java.util.regex.*;

class ExtractNumber
{
    public static void main(String[] args)
    {
        String str = "I'm 53.2 km away";
        String[] s = str.split(" ");
        Pattern p = Pattern.compile("(\\d)+\\.(\\d)+");
        double d;
        for(int i = 0; i< s.length; i++)
        {
            Matcher m = p.matcher(s[i]);
            if(m.find())
                d = Double.parseDouble(m.group());
        }
        System.out.println(d);
    }
}

Upvotes: 3

Suraj Chandran
Suraj Chandran

Reputation: 24791

You can directly use a Scanner which has a nextDouble() and hasNextDouble() methods as below:

        Scanner st = new Scanner("I'm 53.2 km away");
        while (!st.hasNextDouble())
        {
            st.next();
        }
        double value = st.nextDouble();
        System.out.println(value);

Output: 53.2

Upvotes: 9

nbarraille
nbarraille

Reputation: 10023

If you know for sure your numbers are "words" (space separated) and don't want to use RegExs, you can just parse them...

String myString = "I'm 53.2 km away";
List<Double> doubles = new ArrayList<Double>();
for (String s : myString.split(" ")) {
    try {
        doubles.add(Double.valueOf(s));
    } catch (NumberFormatException e) {
    }
}

Upvotes: 0

dantuch
dantuch

Reputation: 9283

Here is good regex site with tester:

http://gskinner.com/RegExr/

this works fine \d+\.?\d+

Upvotes: 3

Related Questions