简体   繁体   中英

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;
    }
});

It looks like the double assigned in line

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

contains a comma ( , , ie 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 .

ps : 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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM