diff --git a/src/text.cpp b/src/text.cpp index 7c8ba083f..c5e141e45 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -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 (); } diff --git a/test/urgency.t b/test/urgency.t index 733817a3a..05061060a 100755 --- a/test/urgency.t +++ b/test/urgency.t @@ -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' &&