boost string format question

I I need to format a string so that it is only a certain amount of characters long.

I have a function that returns an float which can be any number at all.
e.g.
1.123456
23.12345
0.123
123.123

I am using boost and I am doing this:
1
2
3
4
void myFunction( float myFloat)
{
   std::string tmp = boost::str(boost::format("%1%")%myFloat);
}

The above will result in no formatting at all.
How do I format my number so that it is only ever 3 characters before the decimal point and 2 after the decimal point. i.e my string will only ever be 6 characters long (including the decimal point)

many thanks
boost::format( "%3.2f%" )

or

boost::format( "%|3.2f|" )

however keep in mind that if the number actually has 4 digits to the left of the decimal point, you'll get all 4, which
will blow your 6-character limit. You'll need to postprocess that (and deal with a floating point number that has
7 digits to the left of the decimal).
thanks jsmith that now works.

But, why, if the number has got 4 digits will the 4 digits be included in the format, should it not truncate it??

thanks

Of what use would boost::format be if it truncated? Would you want it to truncate 1234567 to 123456 or 234567,
neither of which is even close to the number you asked it to output?

Topic archived. No new replies allowed.