user4307551
user4307551

Reputation:

Illegal Number Format Exception in Java

I have this code, which pulls in a double value from an external method call, converts it into a String and then displays it in a JLabel.

My problem is that sometimes the value might display: 1.95000000000000001, and I want to reduce that to 1.95. When I run the application, it prints: Illegal Number Format Exception. Any help would be greatly appreciated.

 if (query.equals("BREAD602")) {

            callMethod.findBread();
            totalPrice = Double.toString(callMethod.totalPriceMethod());
            totalPriceLabel.setText(String.format("%.2f", "   Totalprice to pay: £" + totalPrice));

Upvotes: 0

Views: 942

Answers (1)

dimm
dimm

Reputation: 1798

Use this

 totalPriceLabel.setText(String.format("Totalprice to pay: £%.2f", totalPrice));

Upvotes: 1

Related Questions