如何在C ++中显示浮点值的多个前导零? [英] How to display multiple leading zeros for floating point values in C++?

查看:100
本文介绍了如何在C ++中显示浮点值的多个前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++程序中,我想显示一列浮点值,以便符号,数字和小数点全部对齐。必要时,多个前导零应填充每个值的整数部分。例如:

In a C++ program, I want to display a column of floating point values so that the sign, digits, and decimal point all line up. Multiple leading zeros should pad the whole number part of each value, when necessary. For example:

A column of floating point values:
  +000.0012
  -000.0123
  +000.1235
  -001.2346
  +012.3457
  -123.4568

I有一个经过精心评论的测试程序来演示该问题。但是,当我编辑这篇文章时,在这里找到了我需要的答案:

-在打印浮点数时,前导零

I had an elaborately commented test program that demonstrated the problem. But, as I was editing this post, I found the answer I need here:
- Extra leading zeros when printing float using printf?

基本问题是我使用的格式代码为%+ 04.4f 当我应该使用%+ 09.4f 时,因为我想要的总字段宽度为9:

The essential problem was that I was using a format code of "%+04.4f" when I should use "%+09.4f", because the total field width I want is 9:


  • 1表示符号

  • 3表示整数

  • 1表示小数点

  • 4个小数位数

  • 1 for the sign
  • 3 for the whole digits
  • 1 for the decimal point
  • 4 for the fractional digits

我没有足够的声誉分数可以评论该帖子,所以谢谢从这里开始,@ AndiDog。

I do not have enough reputation points to comment on that post, so thank you from here, @AndiDog.

我仍然不知道如何仅使用流格式标志来获取多个前导零。但这是另一场战斗。

I still do not know how to get multiple leading zeros using just stream formatting flags. But that is a battle for another day. I will stick with a mixture of printf and stream for now.

推荐答案

一些评论提到了 std :: setfill('0') std :: setw 。尽管这些是必需的,但它们不足以完成任务。例如,这段代码:

A couple of comments have mentioned std::setfill('0') and std::setw. While these are necessary, they're not sufficient to the task. For example, this code:

std::cout << std::setfill('0') << std::setw(7) << std::showpos << 0.012;

会产生: 0 + 0.012 作为其输出。显然,这并不是我们想要的。

will produce: 0+0.012 as its output. This is obviously not quite what we wanted.

我们需要添加 std :: internal 标志来告知要插入内部填充的流,即应在符号和数字的其余部分之间插入填充,因此代码如下:

We need to add the std::internal flag to tell the stream to insert "internal padding" -- i.e., the padding should be inserted between the sign and the rest of the number, so code like this:

std::cout << std::setfill('0') << std::setw(7) << std::internal << std::showpos << 0.012;

...产生我们想要的输出: +00.012

...produces the output we want: +00.012.

还要注意,填充字符是粘滞的,因此如果在 std :: setw 具有数字和非数字类型,您可能需要/想要每次更改它。否则,类似 std :: cout<< setw(12)<<名称; 会产生如下结果: 0000000Jerry ,也很少有人希望这样做。

Also note that the padding character is "sticky", so if you alternate between using std::setw with numeric and non-numeric types, you'll probably need/want to change it each time. Otherwise, something like std::cout << setw(12) << name; will produce results like: 0000000Jerry, which is rarely desired either.

为了确保小数点后总是得到相同的位数,我们还需要设置 std :: fixed 标志,并使用<$指定位数c $ c> std :: setprecision ,例如:

To assure that we always get the same number of places after the decimal point, we also need to set the std::fixed flag, and specify the number of places with std::setprecision, such as:

#include <iostream>
#include <iomanip>
#include <vector>

int main() {
    std::vector<double> values { 0.1234, 1.234, 1.5555 };

    for (auto d : values)
        std::cout << std::internal << std::showpos << std::setw(9) 
                  << std::setprecision(3) << std::setfill('0') << d << "\n";
}

我希望得到哪个输出:

+0000.123
+0001.234
+0001.556

在一种情况下,您将无法以这种方式获得对齐结果:如果您的数字太大而无法容纳在提供的字段中,则小数点前的所有位置仍被打印。例如,如果我们将 1e10 添加到上述代码要打印的数字列表中,它将被打印为: +10000000000.000 ,显然与其余部分不符。

There is one circumstance under which you won't get aligned results this way though: if you have a number too large to fit into the field provided, all the places before the decimal point will still be printed. For example, if we added 1e10 to the list of numbers to be printed by the preceding code, it would be printed out as: +10000000000.000, which obviously won't align with the rest.

处理该问题的明显方法是忍受,如果有的话经常会引起人们的关注,增加字段的大小以容纳更大的数字。

The obvious way to deal with that would be to just put up with it, and if it arises often enough to care about, increase the field size to accommodate the larger numbers.

另一种可能性是,仅当数字在某个阈值以下时,才使用固定符号。

Another possibility would be to use fixed notation only the number is below a certain threshold, and switch to (for example) scientific notation for larger numbers.

至少以我的经验,这样的代码往往主要用于财务数据,在这种情况下,后者选项通常是不可接受的。

At least in my experience, code like this tends to be used primarily for financial data, in which case the latter option usually isn't acceptable though.

这篇关于如何在C ++中显示浮点值的多个前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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