Boost logo

Boost Users :

Subject: [Boost-users] [format] print float number with fixed number of decimals *and* integer digits
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-09-30 19:21:37


I always found the rules for printf formatting, which Boost.Format
uses, cryptic. I usually find the right combination by trial and
error. In this case I want to print decimal numbers with both fixed
number of integer digits and decimal digits. How can I do this with
Boost.Format.

#include<boost/format.hpp>
#include<iostream>
int main(){
  int i = 2;
  std::cout << boost::format("%04i") % i << std::endl; //prints 0002,
ok!
  double d = 3.1415;
  std::cout<< boost::format("%04.8f") % d << std::endl;
  // ^ prints 3.14150000,
  // want 0003.14150000
  //(or even ' 3.14150000')
  //(or even ' 3.1415 ')
  return 0;
}

Thank you,
Alfredo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net