Preventing numbers from showing up in scientific notation We have a StreamBuffer class which is not inherited from std::ios_base (or some of it's derivatives such as std::ostream) so that I can use std::fixed operations and I am trying to prevent number showing up in scientific notations. With my below code some numbers are getting shown in scientific notations. We want to avoid doing any allocations so that's why we implemented StreamBuffer class because of performance reason.

T value = 0;

template&lt;typename U&gt; void process(U&amp; buf, DataOption holder) const {
    if (holder == DataOption::TYPES) {
        switch (type_) {
        case teck::PROC_FLOAT:
            buf &lt;&lt; "{\"float\":" &lt;&lt; value &lt;&lt; "}";
            break;
        case teck::PROC_DOUBLE:
            buf &lt;&lt; "{\"double\":" &lt;&lt; value &lt;&lt; "}";
            break;
        default:
            buf &lt;&lt; "{\"" &lt;&lt; type_ &lt;&lt; "\":" &lt;&lt; value &lt;&lt; "}";
        }
    }
}


This is the way it is being called:

void HolderProcess::dump(std::ostream&amp; os, DataOption holder) const 
{
    process&lt;std::ostream&gt;(os, holder);
}

void HolderProcess::dump(StreamBuffer&amp; buffer, DataOption holder) const
{
    process&lt;StreamBuffer&gt;(buffer, holder);
}


I came up with this solution to avoid number (here value is coming in exponent form and I want to have a fixed number for that value) getting converted to scientific notation, but it uses string and it will do some allocations, which is not performance-efficient.

template &lt;typename T&gt; string str(T number)
{
   std::ostringstream ss;
   ss &lt;&lt; std::fixed &lt;&lt; number;
   return ss.str();
}


And then I can call above template method from my above each case block. This, however, will most certainly allocate more memory. Is there any better way to solve this problem which is optimized, efficient and doesn't do any allocations?
You could use vsnprintf if it's available on your system.  It might seem like a step backwards to use a C-style printf derivative in C++ code, but that's what's used in gcc on my Linux machine.  You can use alloca which allocates space on the stack, or a fixed size (but guaranteed sufficient) local array.

Because your StreamBuffer is not derived from std::ios_base and you haven't provided details of its implementation, I can't really show a code example but I presume that the StreamBuffer&amp; operator&lt;&lt;() either is already or can be adapted to use vsnprintf.