stephensonpj
stephensonpj

Reputation: 37

Display two decimal places in java using printf?

A sample of the code below

double maxHeight;
maxHeight = (Math.pow(speed * Math.sin(angle), 2) / 2 * gravity);
System.out.printf("Maximum height: %.2f", (maxHeight));

The error i receive:

"The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, double)"

What is the simplest solution to printing only two decimal places using printf method or are there any other solutions without creating a decimalFormat?

Upvotes: 0

Views: 5444

Answers (1)

The problem is that varargs is looking for one or more Objects, and you provided a primitive variable. The simplest method is probably just to wrap it:

System.out.printf("Maximum height: %.2f", Double.valueOf(maxHeight));

Upvotes: 3

Related Questions