c++ - C ++ cout将双精度对齐到2并右对齐
问题描述
如何将代码对齐到右侧的 3 个位置而不是默认的左侧?
10.23
100.23
10.23 ----------Like this
100.23
double test = 10.2345;
double test2 =100.2345;
std::cout << std::setprecision(2) << std::fixed << test << '\n' << test2 << std::endl;
解决方案
首先使用设置输出的宽度,然后使用std::cout.width(num);
设置输出到右侧std::right
,如下所示:
#include <iostream>
#include <iomanip>
int main()
{
std::cout.width(6);
std::cout << std::right << 10.23 << std::endl;
std::cout << std::right << 10.453;
return 0;
}
推荐阅读
- javascript - 如何在表单提交时显示服务器响应?
- javascript - Javascript中浮点数的最大精度(小数点后)是多少
- redirect - TTFB 是否包括重定向到我的页面的请求的时间?
- c++ - 如何将 GDI+ HDC 保存为位图文件?
- git - scm git checkout 期间无法识别 GERRIT_REFSPEC - Jenkinsfile 声明
- nestjs - 可以使用 Nestjs 的异常过滤器处理多个错误吗?
- html - Bootstrap:两行而不是一行的文本
- tensorflow - tf.add() 和 tensorflow.keras.layers.Add() 的区别
- redux - next-redux-wrapper Wrapper.getStaticProps 无法使用
- php - 从外部网站获取元素内的图像 URL - Laravel