Reputation: 37
How can i return back the double value with 2 decimals place in Java program?
public String toString()
{
return "\nCost: $" +computeRentalCost() ;
}
Upvotes: 0
Views: 86
Reputation: 34146
You can use String.format()
:
return String.format("\nCost: $%.2f", computeRentalCost());
The format modifier %.2f
tells that only two decimal places will be shown.
Note:
Upvotes: 2