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
  • 8,585
  • 14
  • 54
  • 67
ter
  • 11
  • 5
  • It did not work. Still gives same error – ter May 11 '17 at 02:51
  • 1
    Possible duplicate of [How to display an output of float data with 2 decimal places in Java?](http://stackoverflow.com/questions/2538787/how-to-display-an-output-of-float-data-with-2-decimal-places-in-java) – Pedro Hidalgo May 11 '17 at 02:55
  • @PedroHidalgo I think OP has `double` data – Kaushal28 May 11 '17 at 02:56
  • @Kaushal28 ok, it could be a duplicated of this one I think: http://stackoverflow.com/questions/8819842/best-way-to-format-a-double-value-to-2-decimal-places This is what he is looking for. – Pedro Hidalgo May 11 '17 at 03:03
  • Your code doesn't show any formatted output commands. – Lew Bloch May 11 '17 at 03:32

2 Answers2

1

You can format your string like:

NumberFormat formatter = new DecimalFormat("#0.00"); 
System.out.println(formatter.format((Double.parseDouble(".04") * aLONGTYPEnumber * 24)));
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
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

River
  • 8,585
  • 14
  • 54
  • 67