#include <iostream>
#include <format>
#include <cmath>
int main()
{
double pricipal{ 1000.00 };
double rate{ 0.05 };
std::cout << std::format("Initial Pricipal: {:>7.2f}\n", pricipal);
std::cout << std::format("Initial Rate: {:>7.2f}\n", rate);
std::cout << std::format("\n{}{:>20}\n", "Year", "Amount on deposit");
for (int year{1}; year <= 10; ++year)
{
double amount = pricipal * pow(1.0 + rate, year);
std::cout << std::format("{:>4d}{:>20.2f}\n", year, amount);
}
}
{:>7.2f} 的意思是
‘:’代表开始声明格式
‘>’ 代表右对齐
7 代表占7个字符位置大小,用空格填充
.2f代表保留两位小数