Search code examples
javadoublenumber-formatting

Problems after converting string to double


I am fairly new to java and as such I have no earthly idea why this is not working. Could you please tell me why I am getting this error?

I have already imported the java.text.NumberFormat.

NumberFormat fmt1 = NumberFormat.getcurrencyInstance();
String r = "12579500";
double s = Double.parseDouble(r);
double t = fmt1.format(s);

When compiling I get: incompatible types:

string cannot be converted to double


Solution

  • fmt1.format(s) returns a String (the formatted String according to the NumberFormat).
    double and String are incompariable types.

    final String formatted = fmt1.format(s);
    

    BTW, you've made a typo in NumberFormat.getCurrencyInstance().