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 ();
}

View file

@ -28,7 +28,7 @@
use strict;
use warnings;
use Test::More tests => 48;
use Test::More tests => 49;
# Create the rc file.
if (open my $fh, '>', 'urgency.rc')
@ -310,6 +310,11 @@ qx {../src/task rc:urgency.rc add 12b scheduled:yesterday 2>&1};
$output = qx{../src/task rc:urgency.rc 45 _urgency 2>&1};
like ($output, qr/urgency 5$/ms, 'scheduled past = 5');
# urgency values between 0 and 1
qx {../src/task rc:urgency.rc add 13 pri:H 2>&1};
$output = qx{../src/task rc:urgency.rc rc.urgency.priority.coefficient:0.01234 46 info 2>&1};
like ($output, qr/Urgency 0.01$/ms, 'near-zero urgency is truncated');
# Cleanup.
unlink qw(pending.data completed.data undo.data backlog.data synch.key urgency.rc);
ok (! -r 'pending.data' &&