-2

Im having trouble to find a way for formatting this:

System.out.println("Product price reported as $" + price + " before tax and $" + result + " after " + this.tax + " % tax"):

Im trying like this.

String formatDecimals = String.format("%.2f","Product price reported as $" + price + " before tax and $" + result + " after " + this.tax + " % tax"):

I want to have two decimals. Any help

Stefan
  • 7
  • 1
  • 2
    what is the problem you are having? what datatypes are they? – Stultuske Aug 08 '22 at 11:35
  • either way, if you want formatted output, try printf : printf("%. 2f", val) – Stultuske Aug 08 '22 at 11:36
  • `double`, it says `argument type String does not match the type of the format specifier` – Stefan Aug 08 '22 at 11:36
  • `"%.2f"` only accepts a numerical value. You supply the whole string. – XtremeBaumer Aug 08 '22 at 11:37
  • Does this answer your question? [How to print a float with 2 decimal places in Java?](https://stackoverflow.com/questions/2538787/how-to-print-a-float-with-2-decimal-places-in-java) – Chaosfire Aug 08 '22 at 11:38
  • What is the problem you are having? What are the data-types of the variables? Give an example of what it should look like and how it is actually appearing. Edit: forgot the obligatory comment that you shouldn't use floating point for money. :) – vsfDawg Aug 08 '22 at 11:56

1 Answers1

2

You are trying to format a string as a float, this will not work. The whole text has to go into the "format" parameter:

String formatDecimals = String.format("Product price reported as $%.2f before tax and $%.2f after %.2f %% tax", price, result, this.tax);
cyberbrain
  • 3,433
  • 1
  • 12
  • 22