Boost logo

Boost Users :

Subject: Re: [Boost-users] [format] print float number with fixed number of decimals *and* integer digits
From: Larry (lknain_at_[hidden])
Date: 2010-09-30 22:45:52


I don't know the origin of the specification although I suspect it may have
come from or been influenced by FORTRAN (or maybe COBOL although I have
forgotten anything I knew about COBOL). FORTRAN has always done it this way
and it predates C and C++ by a few years (doesn't make it right, I know):
that is, the "15" is the field width and the ".8" is the number of decimal
digits. It sort of makes sense. In the %04d case 4 is the field width with
perhaps an implied .0 since integers by definition have no decimal part. You
can, of course, specify %04.0lf and will get an integer representation of a
double value with a field width of 4.

Larry
----- Original Message -----
From: "Alfredo Correa" <alfredo.correa_at_[hidden]>
Cc: <boost-users_at_[hidden]>
Sent: Thursday, September 30, 2010 9:33 PM
Subject: Re: [Boost-users] [format] print float number with fixed number of
decimals *and* integer digits

On Thu, Sep 30, 2010 at 5:41 PM, Larry <lknain_at_[hidden]> wrote:
> I believe if you use something like "%015.8lf" you will get more of what
> you
> want.
> I always think that the .8 is the number of digits to the right of decimal
> and the 15
> is the entire field width (including sign, any leading zero digits, any
> digits to the left
> of the decimal, the decimal point, and digits to the right of decimal
> point).

Right, I though that the number before the dot . was the number of
integer digits not the total number of digits.

  std::cout<< boost::format("%015.8f") % d << std::endl;

works, prints 000003.14150000

Thank you,
Alfredo

>For
> example:
>
> double pi(3.141592);
>
> format a("%015.8lf");
> a % pi;
> printf("%s\n",a.str().c_str());
>
> prints:
>
> 000003.14159200
>
> Larry
> ----- Original Message ----- From: "alfC" <alfredo.correa_at_[hidden]>
> To: <boost-users_at_[hidden]>
> Sent: Thursday, September 30, 2010 7:21 PM
> Subject: [Boost-users] [format] print float number with fixed number of
> decimals *and* integer digits
>
>
>> 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 mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
>
>
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


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