How can i return back the double value with 2 decimals place in Java program?
public String toString()
{
return "\nCost: $" +computeRentalCost() ;
}
How can i return back the double value with 2 decimals place in Java program?
public String toString()
{
return "\nCost: $" +computeRentalCost() ;
}
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: