0

I'm getting an error while trying to format a string number:

Argument type 'String' does not match the type of the format specifier '%.2f'.

The String is created by:

String cost = Double.parseDouble((Double.parseDouble(".04") * aLONGTYPEnumber * 24));
River
9,20115 gold badges57 silver badges69 bronze badges
asked May 11, 2017 at 2:46
5
  • It did not work. Still gives same error Commented May 11, 2017 at 2:51
  • 1
    Possible duplicate of How to display an output of float data with 2 decimal places in Java? Commented May 11, 2017 at 2:55
  • @PedroHidalgo I think OP has double data Commented May 11, 2017 at 2:56
  • @Kaushal28 ok, it could be a duplicated of this one I think: stackoverflow.com/questions/8819842/… This is what he is looking for. Commented May 11, 2017 at 3:03
  • Your code doesn't show any formatted output commands. Commented May 11, 2017 at 3:32

2 Answers 2

1

The problem is you're trying to print a String using a double specifier (%.2f).

Using:

System.out.printf("%.2f", d);

requires that d is a double or Double, while your cost is a String.

Try it with:

double cost = Double.parseDouble(".04") * aLONGTYPEnumber * 24;

and it should work

answered May 11, 2017 at 2:56
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You so much it worked. I don't know what i was thinking.
1

You can format your string like:

NumberFormat formatter = new DecimalFormat("#0.00"); 
System.out.println(formatter.format((Double.parseDouble(".04") * aLONGTYPEnumber * 24)));
answered May 11, 2017 at 2:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.