简体   繁体   中英

Align decimal places in output?

#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;
}

Output given:

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

Output desired:

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

I also need to have the colons align as such.

You will have to put proper setw in different parts as well as left align based on your text

1) First part

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

2) Second part assume it to be of approx length 30

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

3) To align colon :

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

4) value part

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;
}

output

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

As far as I know there is no fast way to do that using isstream/iomanip

Precision doesn't define length of fractional part but number of all digits.

I understand, you need to pad values correctly. In this case, solution is sprintf from [cstdio]. It should look something like this:

sprintf(YourBuffer, "%10.3f", YourVariable);

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

http://www.cplusplus.com/reference/cstdio/printf/ - short version

UPDATE: As I saw you didn't want just the second field to align. But if you are hard wiring the fields, you can format those yourself. If passed to you as strings, they can be handled with the same method as your doubles.

As you want to align the decimal points on the results, you have to do that yourself from what I understand. A helper structure keeps it out of the way and reusable.

    #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;
}

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM