user5000
user5000

Reputation: 37

return back the double value with 2 decimals place in Java program

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

Answers (1)

Christian Tapia
Christian Tapia

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:

  • This won't modify the number, it only modifies the way it's shown.

Upvotes: 2

Related Questions