if(w>=10)
{
long double div = 1;
- while(div*10<w)
+ while(div*10<=w)
{
++exp;
div *= 10;
// Decide how to format the number
unsigned digits;
+ unsigned leading_zeroes = 0;
unsigned point = 1;
bool showexp = false;
if(mode==Fmt::FIXED)
}
else
{
- point = max(exp, 0)+1;
if(exp<0)
- digits += -exp;
+ leading_zeroes = -exp;
+ else
+ point = exp+1;
}
}
// Apply rounding
w += 5.0l/pow(10.0l, static_cast<long double>(digits));
- if(w>10)
+ if(w>=10)
{
// Rounding bumped us to the next exponent, deal with it
w /= 10;
}
if(!showexp)
{
- ++digits;
- ++point;
+ if(mode==Fmt::FIXED)
+ ++digits;
+ if(leading_zeroes)
+ --leading_zeroes;
+ else
+ ++point;
}
else
++exp;
}
+ digits += leading_zeroes;
+
// Create a buffer and start from the end
unsigned size = max(f.get_width(), digits+8);
char *buf = new char[size];
{
if(i==point)
*mptr++ = '.';
- if(showexp || static_cast<int>(i)>=-exp)
+ if(!leading_zeroes)
{
int digit = static_cast<int>(w);
*mptr++ = '0'+digit;
w = (w-digit)*10;
}
else
+ {
*mptr++ = '0';
+ --leading_zeroes;
+ }
}
if(f.get_showpoint())