Convert a string to a double for a if clause

74 Views Asked by At

I'm trying to convert a string to a double, so I can use the double to compare it with another one, to make the result green if it's the same or red when not.

The first method is:

public void getCords(View view) {

    double latitude;
    double longitude;

    latitude = 1.00;
    longitude = 0.00;

    String stringlatitude = String.valueOf(latitude);
    String stringlongitude = String.valueOf(longitude);

    TextView koordinaten_lat = (TextView) findViewById(R.id.koordinaten_lat);
    TextView koordinaten_long = (TextView) findViewById(R.id.koordinaten_long);

    koordinaten_lat.setText(stringlatitude);
    koordinaten_long.setText(stringlongitude);

    String latitude2 = DataHolder.getInstance().getLatitude();
    String longitude2 = DataHolder.getInstance().getLongitude();

    int compare = Double.compare(latitude, latitude2);
    if(compare > 0) {
        // paint text red
    }
    else if(compare < 0) {
        // paint text red
    }
    else {
        // paint text green
    }
}

I take the Data from a DataHolder where the Source-Code is:

public class DataHolder {

private String latitude;
public String getLatitude() {return latitude;}
public void setLatitude(String stringLatitude) {this.latitude = stringLatitude;}

private String longitude;
public String getLongitude() {return longitude;}
public void setLongitude(String stringLongitude) {this.longitude = stringLongitude;}


private static final DataHolder holder = new DataHolder();
public static DataHolder getInstance() {return holder;}

}

Which takes it from the main Setting:

public void getCords (View view) {

        Intent intent1 = new Intent(this, MainActivity.class);

        double latitude;
        double longitude;

        latitude = 0.00;
        longitude = 0.00;

        String stringlatitude = String.valueOf(latitude);
        String stringlongitude = String.valueOf(longitude);

        TextView koordinaten_lat = (TextView) findViewById(R.id.koordinaten_lat);
        TextView koordinaten_long = (TextView) findViewById(R.id.koordinaten_long);

        koordinaten_lat.setText(stringlatitude);
        koordinaten_long.setText(stringlongitude);

        DataHolder.getInstance().setLatitude(stringlatitude);
        DataHolder.getInstance().setLongitude(stringlongitude);
    }
1

There are 1 best solutions below

0
On BEST ANSWER

try this

 String text = "your string"; // example

  double value =

 Double.parseDouble(text);

hope it helps