+1  A: 

Is that what you want? Here, we calculate the amount of available precision and set ostream accordingly.

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main(int argc, char* argv[])
    {
    // Input
    double value =  102.1213239999;
    // Calculate limits
    int digits = ( (value<1) ? 1 : int(1+log10(double(abs(value)))) );
    int width = 10;
    int precision = (((width-digits-1)>=0) ? (width-digits-1):0);

    // Display
    cout.setf(ios::fixed);
    cout.precision(precision);
    cout<<setw(10)<<value<<endl;


    return 0;

    }
    OUTPUT: 102.121324

Btw, if you want a truckload of ways to compute digits, here's how.

Jacob
OP wants maximum output of 10 chars with maximum precision.
mobrule
What happens when value > 1e9?
Jacob
How about now? It will not stay at 10 chars if it's greater than 1e9 but the precision problem is resolved (I think).
Jacob
+1  A: 

How about lexical cast?

double x = 102.1213239999;
std::cout << boost::lexical_cast<std::string>(x).substr(0,10);

Its not exactly what you asked for. I was just trying to think outside the box.
You may also want to look at this question for a discussion on formatting differences between C and C++ and check out the Boost Format Library

Martin York
This is wrong. Consider this number:double x = 12345678901.Your answer is going to be off by orders of magnitude.
Vitali
Even though the example is wrong, the suggestion to look at the Boost Format library is worth a +1.
MP24
@unknown: I find absolute statements are usually wrong (this is probably not a good solution). I know that big values will be truncated, but the question is about fitting the data into a fixed size output field not accuracy (otherwise the solution is trivial use the manipulator width(10) ). In the original question snprint() is used to truncate the output, this alternative provides an equivalent (but neater) solution in C++. The real question is what do you do when you have a fixed size output field and the value is larger! This is why width(x) specifies a minimum width not a maximum width.
Martin York
A: 

You didn't specify the language, so here goes the python solution:

d=123.12345678910111213 '%.10f'%d

this will give you: '123.1234567891'

fccoelho
-1. Its is mentioned in the question as well in the tags.
Ganesh M
A: 

You can use osteram::width and ostream::precision function to achieve your goal, like this

std::ostringstream out;
out.width(10);
out.precision(10);
out << 123.12345678910111213;

Although it won't add zeros after the point in order to respect width but it will add spaces (or any character of you choise) before the number. So You'll get ' 102' or '0000000102' (if you call out.fill('0');) instead of '102.000000' if you pass 102 as a input value.

Serge
+1  A: 

Use boost::lexical_cast

Davit Siradeghyan