std :: ostream的浮点格式 [英] Floating point format for std::ostream

查看:166
本文介绍了std :: ostream的浮点格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用std :: cout执行以下操作?

How do I do the following with std::cout?

double my_double = 42.0;
char str[12];
printf_s("%11.6lf", my_double); // Prints " 42.000000"

我正准备放弃并使用sprintf_s。

I am just about ready to give up and use sprintf_s.

更一般地说,我在哪里可以找到一个std :: ostream格式化的引用,它列出了一个地方的所有内容,而不是在一个长的教程中传播?

More generally, where can I find a reference on std::ostream formatting that lists everything in one place, rather than spreading it all out in a long tutorial?

推荐答案

std::cout << std::fixed << std::setw( 11 ) << std::setprecision( 6 ) << my_double;

您需要添加

#include <iomanip>

您需要

You need stream manipulators

你可以用任何你想要的字符填充空白的地方。像这样:

You may "fill" the empty places with whatever char you want. Like this:

std::cout << std::fixed << std::setw( 11 ) << std::setprecision( 6 ) 
          << std::setfill( '0' ) << my_double;

这篇关于std :: ostream的浮点格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆