如何使用 std::format 创建宽度和声明精度(C++20)?
How to use std::format to create width and declare precision(in C++20)?
我正在尝试在 C++20 中使用#include <格式> 创建一个 table。但是我不知道如何同时创建 17 的中心对齐和 2 位小数的精度。我知道 17 中心对齐的宽度很简单: cout<
这是我的代码片段:
for (size_t k{};k<length;++k)
{
cout << "Enter the product number:\n";
cin >> value;
product.push_back(value);
cout << "Enter the quantity:\n";
cin >> value;
quantity.push_back(value);
cout << "Enter the unit price($):\n";
cin >> value;
price.push_back(value);
}
cout << "Product Quantity Perunit($) Cost($)" << endl;
for (int i = 0; i < product.size(); i++)
{
cout << format("{:<}{:^15}{:^7}{:^17.2}\n",product[i],
quantity[i],price[i],(quantity[i]*price[i]));
}
我试过做 {:^17.2} 但什么也没做(看图片)。请指教。
尝试包含 f
说明符(对于浮点数):{:^17.2f}
我正在尝试在 C++20 中使用#include <格式> 创建一个 table。但是我不知道如何同时创建 17 的中心对齐和 2 位小数的精度。我知道 17 中心对齐的宽度很简单: cout< 这是我的代码片段: 我试过做 {:^17.2} 但什么也没做(看图片)。请指教。
for (size_t k{};k<length;++k)
{
cout << "Enter the product number:\n";
cin >> value;
product.push_back(value);
cout << "Enter the quantity:\n";
cin >> value;
quantity.push_back(value);
cout << "Enter the unit price($):\n";
cin >> value;
price.push_back(value);
}
cout << "Product Quantity Perunit($) Cost($)" << endl;
for (int i = 0; i < product.size(); i++)
{
cout << format("{:<}{:^15}{:^7}{:^17.2}\n",product[i],
quantity[i],price[i],(quantity[i]*price[i]));
}
尝试包含 f
说明符(对于浮点数):{:^17.2f}