Ankit Mishra
Ankit Mishra

Reputation: 409

How to print floating point number in C++ similar to C

I want to print my data in pdb format which is a specific format of storing atomic coordinates so that they can be read by some standard molecular visualisation softwares.

Currently I am using a work around regular C++ and combining my std::cout with printf to get a desired formatted output like this,

std::cout << std::setw(6) << "ATOM" << std::setw(5) << "0" << " " 
          <<  std::setw(4) << "C" << std::setw(12) << global_id
          << "    ";
 printf("%8.3f %8.3f %8.3f %6.2f %6.2f \n", pos[0], pos[1], pos[2], tt, ss );

where global_id is an integer.

So how can I eliminate this printf and write the entire statement just by using std::cout with specified precision before and after decimal point.

Any help will be greatly appreciated.

Upvotes: 0

Views: 396

Answers (1)

Mikel F
Mikel F

Reputation: 3656

The mechanism you are looking for is setprecision() as described here: http://en.cppreference.com/w/cpp/io/manip/setprecision

In conjunction with setw():

http://en.cppreference.com/w/cpp/io/manip/setw

Upvotes: 2

Related Questions