首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用std::format创建宽度和声明精度(在C++20中)?

如何使用std::format创建宽度和声明精度(在C++20中)?
EN

Stack Overflow用户
提问于 2021-07-19 05:07:06
回答 1查看 50关注 0票数 0

我正尝试在C++20中使用#include < format >创建一个表,但是我不知道如何同时创建17位的居中对齐和2位小数的精度。我知道17居中对齐的宽度很简单: cout<

这是我的代码片段:

代码语言:javascript
运行
复制
      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},但什么也没做(请看图片)。请多多指教。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-19 05:11:03

尝试包含f说明符(用于浮点数):{:^17.2f}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68433089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档