1

In front I set the text like that with the priceFormat being S$%.2f.

textPrice.setText(String.format(priceFormat, item.getPrice()));

Now I want to convert it to a double variable which I definitely think I have to make use of the priceFormat but I have no idea how to. This bottom line is wrong.

double Price=Double.parseDouble(textPrice.getText());
Derek Toh
  • 33
  • 3
  • 10

2 Answers2

0

You need to convert the textPrice.getText() to a String since its Double.parseDouble(String):

double price = Double.parseDouble(mStatus.getText().toString());

You also have to eliminate the S$ and the trailing .:

double price = Double.parseDouble(mStatus.getText().toString().replaceAll("S\\$|\\.$", ""));

Of course you should make this less error-prone:

double price = 0d;
try {
    price = Double.parseDouble(mStatus.getText().toString().replaceAll("S\\$|\\.$", ""));
}
catch (NumberFormatException e) {
    // show an error message to the user
    textPrice.setError("Please enter a valid number");
}
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
-1

you need to remove that S$ before parsing, one of the way is:

String text = textPrice.getText();
String priceText = text.split("$")[1].trim(); //splitting numeric characters with the currency characters
double priceVal = Double.parseDouble(priceText); //parsing it to double