Enhancement

- When formating real numbers that are between 0 and 1, the width controls the
  number of characters that are shown (with width "4", "0.01234" is shown as
  "0.01").
This commit is contained in:
Louis-Claude Canon 2012-07-27 00:30:39 +02:00 committed by Paul Beckingham
parent afcbaa20a9
commit 0ac6578899
2 changed files with 25 additions and 1 deletions

View file

@ -36,6 +36,7 @@
#include <strings.h>
#include <ctype.h>
#include <Context.h>
#include <math.h>
#include <util.h>
#include <text.h>
#include <utf8.h>
@ -857,6 +858,15 @@ const std::string format (float value, int width, int precision)
std::stringstream s;
s.width (width);
s.precision (precision);
if (0 < value && value < 1)
{
// For value close to zero, width - 2 (2 accounts for the first zero and
// the dot) is the number of digits after zero that are significant
double factor = 1;
for (int i = 2; i < width; i++)
factor *= 10;
value = roundf (value * factor) / factor;
}
s << value;
return s.str ();
}
@ -867,6 +877,15 @@ const std::string format (double value, int width, int precision)
std::stringstream s;
s.width (width);
s.precision (precision);
if (0 < value && value < 1)
{
// For value close to zero, width - 2 (2 accounts for the first zero and
// the dot) is the number of digits after zero that are significant
double factor = 1;
for (int i = 2; i < width; i++)
factor *= 10;
value = round (value * factor) / factor;
}
s << value;
return s.str ();
}