fork download
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. auto to_string_precision(double amount, int precision)
  8. {
  9. stringstream stream;
  10. stream << fixed << setprecision(precision) << amount;
  11. return stream.str();
  12. };
  13.  
  14. int main()
  15. {
  16. double amount = 10000.0f;
  17. double rateMonthly = 0.10;
  18.  
  19. cout << setw(25) << left << "Loan amount:" << setw(10) << right << ("$ " + to_string_precision(amount, 2)) << endl;
  20. cout << setw(25) << left << "Monthly Interest Rate:" << setw(10) << right << (to_string_precision(rateMonthly, 2) + "%") << endl;
  21. cout << endl;
  22. cout << setw(25) << left << "Loan amount:" << "$ " << amount << endl;
  23. cout << setw(25) << left << "Monthly Interest Rate:" << rateMonthly << "%" << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Loan amount:             $ 10000.00
Monthly Interest Rate:        0.10%

Loan amount:             $ 10000
Monthly Interest Rate:   0.1%