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:
RewardsCreditAccount testAccount = new RewardsCreditAccount("Joe F. Pyne", 7384.282343837483298347, 0.173, 567);
So this object will set accountHolder = "Joe F. Pyne", balance = 7384.282343837483298347, and so on.
In the function below, I return this information in a string that should look like this:
Joe F. Pyne, $7384.282, 17.28%, 567 points
Using this function:
public String toString() {
return (accountHolder + ", $" + + balance + 100*interestRate + "%, " + points + " points");
}
However, it is in fact returning this:
Joe F. Pyne, $7384.282, 17.299999999999997%, 567 points
I tried this, but to no avail
public String toString() {
return (accountHolder + ", $" + + ("%,1.2f",balance) + 100*interestRate + "%, " + points + " points");
}
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!