在输出中对齐小数位?

时间:2019-01-17 00:32:31

标签: c++ c++14

#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;


int main() {
    //
    //HERE IS THE ISSUE
    //set precision to 3 decimals
    cout<<fixed;
    //printing the final pressure of the gas
    cout <<setw(20)<<left<<setfill('.')<<"Equation #01"<<"Ideal Gas Law(Chemistry): "<<setw(5)<<setprecision(3)<<gaslawPressure<<" atm" 
<<endl;

    //printing the calculated distance
    cout <<setw(20)<<left<<setfill('.')<<"Equation #02"<<"Distance Formula(Math): "<<setw(5)<<setprecision(3)<<pointDistance<<endl;
    return 0;
}

给出的输出:

Equation #01........Ideal Gas Law(Chemistry): 1.641 atm
Equation #02........Distance Formula(Math): 30.017

所需的输出:

Equation #01........Ideal Gas Law(Chemistry):    1.641 atm
Equation #02........Distance Formula(Math)  :   30.017

我还需要使冒号对齐。

3 个答案:

答案 0 :(得分:1)

您将必须在不同的位置放置适当的setw,并根据文本左对齐

1)第一部分

setw(20)<<left<<setfill('.')<<"Equation #01" 

2)第二部分假定其长度约为30

setw(30)<<left<<setfill(' ')<<"Ideal Gas Law(Chemistry)"

3)对齐冒号:

setw(3)<<left<<setfill(' ')<<":"

4)价值部分

setw(5)<<std::left<<setprecision(3)<<gaslawPressure<<" atm"



#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;


int main() {
    //
    //HERE IS THE ISSUE
    //set precision to 3 decimals
    auto gaslawPressure = 1.641;
    auto pointDistance = 30.017;

    cout<<fixed;
    //printing the final pressure of the gas
    cout <<setw(20)<<left<<setfill('.')<<"Equation #01"<<setw(30)<<left<<setfill(' ')<<"Ideal Gas Law(Chemistry)"<<setw(3)<<left<<setfill(' ')<<":"<<setw(5)<<std::left<<setprecision(3)<<gaslawPressure<<" atm"<<endl;

    //printing the calculated distance
    cout <<std::left<<setw(20)<<left<<setfill('.')<<"Equation #02"<<setw(30)<<left<<setfill(' ')<<"Distance Formula(Math)"<<setw(3)<<left<<setfill(' ')<<":"<<setw(5)<<setprecision(3)<<pointDistance<<endl;
    return 0;
}

输出

Equation #01........Ideal Gas Law(Chemistry)      :  1.641 atm
Equation #02........Distance Formula(Math)        :  30.017
Program ended with exit code: 0

答案 1 :(得分:0)

据我所知,使用isstream / iomanip并没有快速的方法

精度不是定义小数部分的长度,而是所有位数的数字。

我了解,您需要正确填充值。 在这种情况下,解决方案是来自cstdio的sprintf。 它应该看起来像这样:

sprintf(YourBuffer,“%10.3f”,YourVariable);

https://en.cppreference.com/w/cpp/io/c/fprintf

http://www.cplusplus.com/reference/cstdio/printf/-简短版

答案 2 :(得分:0)

更新: 如我所见,您不希望仅第二个字段对齐。但是,如果您很难连接字段,则可以自己格式化这些字段。如果以字符串形式传递给您,则可以使用与双打相同的方法来处理它们。

要在结果上对齐小数点,您必须根据我的理解自己做。辅助结构使它不受影响且可重用。

    #include <iomanip>
    #include <cmath>
    #include <iostream>

    struct buf
    {
        double val;
        buf(double val) :val(val) {}
        friend std::ostream& operator<< (std::ostream& os, buf b) {
            for (double i = b.val; i < 1000; i*=10) os << " ";
            return os << b.val;
        }
    };

int main() {
    //
    double gaslawPressure = 1.615;
    double pointDistance = 221.615;
    std::cout << std::setw(20) << std::left << std::setfill('.')
        << "Equation #01" << "Ideal Gas Law(Chemistry) : " << buf(gaslawPressure)<<" atm" << std::endl;

    //printing the calculated distance
    std::cout << std::setw(20) << std::left << std::setfill('.')
        << "Equation #02" << "Distance Formula(Math)   : "<< buf(pointDistance)<< std::endl;

        return 0;
}

输出:

Equation #01........Ideal Gas Law(Chemistry) :    1.615 atm
Equation #02........Distance Formula(Math)   :  221.615