title: Return a double with only two decimals in Java as part of a larger string?
body: <p>As part of a larger program, I have this function that, when called, returns the string under return. accountHolder, balance, interestRate, and points are variables in an object of type RewardsCreditAccount. So, for example, I declare an object here:</p>
  
  <pre><code>RewardsCreditAccount testAccount = new RewardsCreditAccount("Joe F. Pyne", 7384.282343837483298347, 0.173, 567);
  </code></pre>
  
  <p>So this object will set accountHolder = "Joe F. Pyne", balance = 7384.282343837483298347, and so on.</p>
  
  <p>In the function below, I return this information in a string that should look like this:</p>
  
  <blockquote>
    <p>Joe F. Pyne, $7384.282, 17.28%, 567 points</p>
  </blockquote>
  
  <p>Using this function:</p>
  
  <pre><code>    public String toString() {
      return (accountHolder + ", $" +  + balance + 100*interestRate + "%, " + points + " points");
  }
  </code></pre>
  
  <p>However, it is in fact returning this:</p>
  
  <blockquote>
    <p>Joe F. Pyne, $7384.282, 17.299999999999997%, 567 points</p>
  </blockquote>
  
  <p>I tried this, but to no avail</p>
  
  <pre><code>public String toString() {
      return (accountHolder + ", $" +  + ("%,1.2f",balance) + 100*interestRate + "%, " + points + " points");
  }
  </code></pre>
  
  <p>This is rather annoying, because I want it to only return two decimal places. I know this can be done using %1.2f, but I have no idea how to format the syntax for that. I would vastly appreciate any help on getting the decimal value to display properly. Thanks!</p>