Ptr
Ptr

Reputation: 59

Hot to show only two digits after decimal using double

I use:

priceTextView.setText(String.format("%.2f",price));

price is double. It works but when I have to retrieve the value from the TextView and convert it to double I get the following error:

java.lang.NumberFormatException: Invalid double: "1,2"

Is there another way to do the same thing and avoid the error?

Here is the code complete:

Double prezzoDouble = Double.parseDouble(this.prezzo);
prezzoTextView.setText(String.format("%.2f", prezzoDouble));

quantitaSceltaEditText.addTextChangedListener(new TextWatcher() {

    String prezzoUnitario=prezzo;

    public void afterTextChanged(Editable s) {

        int quantitaSceltaInt = Integer.parseInt(quantitaSceltaEditText.getText().toString());

        if(quantitaSceltaInt>=1) {
            String prezzoTotale = String.valueOf(String.format ("%.2f", (calcoloPrezzoTotale())));
            prezzoTextView.setText(prezzoTotale);
        }
        else
        {
            prezzoTextView.setText(prezzoUnitario);
        }
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    private double calcoloPrezzoTotale()
    {
        double prezzoNum=Double.parseDouble(prezzoTextView.getText().toString()); ///////////
        double prezzoTotale=0;

        int quantitaSceltaNum = Integer.parseInt(quantitaSceltaEditText.getText().toString());

        prezzoTotale = prezzoNum * quantitaSceltaNum;

        return prezzoTotale;
    }
});

Upvotes: 1

Views: 1337

Answers (4)

Ucdemir
Ucdemir

Reputation: 3098

double dTotalPrice = 4.5;
String totalPrice = new DecimalFormat("##.##").format(dTotalPrice);

Upvotes: 0

Taylor Courtney
Taylor Courtney

Reputation: 813

Depending on where you live you could use the Locale.setDefault(Locale) method to change the jvm default which appears to be the issue. It's setDefault (Locale aLocale). This sets a systemwide resource. As per Oracle documentation.

Upvotes: 0

Trinimon
Trinimon

Reputation: 13957

It looks like the double assigned in line

prezzoTotale = String.valueOf(String.format ("%.2f", (calcoloPrezzoTotale())));

contains a comma (,, i.e. 1,2) due to the locale settings. So take care that parsing happens with the same locale.

Class NumberFormat helps you out:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();

Looking at your code it could be Locale.ITALY as well ;) check the full Locale list here.

p.s.: String.format() uses Locale.getDefault(); as per documentation. Use logcat to check its value, if you are not sure about the system setting or use the alternative method public static String format(Locale l, String format, Object... args) which allows you to specify the Locale.

Upvotes: 8

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

You should try the following:

String.format("%.2f",price) //String.format("%2.f",price) is wrong

or

Double.parseDouble(this.prezzo); 

could be a problem decimal separator in this.prezzo, according to the configuration can be a point or comma

Upvotes: 0

Related Questions