[gnumeric] Editing: avoid excess precision for percentages.



commit df9720f2527cfc1d2cdac1f82f9a72396df1139e
Author: Morten Welinder <terra gnome org>
Date:   Thu Oct 31 17:29:21 2019 -0400

    Editing: avoid excess precision for percentages.
    
    This isn't a complete fix, but close enough.
    
    The question we should answer is "what string representing 100*f is
    the shortest, such that when converted to a double and divided by 100
    results in f?"
    
    Close enough here is that we should at least get this right for
    reasonable sized numbers (<10000%) with a most 6 decimals.

 ChangeLog  |  3 +++
 NEWS       |  1 +
 src/cell.c | 10 +++++++---
 3 files changed, 11 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index ec48b9156..cd33af350 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2019-10-31  Morten Welinder  <terra gnome org>
 
+       * src/cell.c (gnm_cell_get_text_for_editing): Avoid excess
+       precision when editing percentages.  [#413]
+
        * src/criteria.c (parse_criteria): Be careful about critiria that
        are either empty values or empty strings.  The semantics are
        special.  [#423]  Fix initial ref-count problem.
diff --git a/NEWS b/NEWS
index 6530db1d0..dd1c0d867 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,7 @@ Morten:
        * Fix problem saving cell protection to xlsx.  [#409]
        * Look for python3 before python.  [#419]
        * Fix SUMIF (etc) problem with blank criteria.  [#423]
+       * Improve editing of percentages.  [#413]
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.45
diff --git a/src/cell.c b/src/cell.c
index 0fc2feafd..4dd1e783c 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -845,9 +845,13 @@ gnm_cell_get_text_for_editing (GnmCell const * cell,
 
                case GO_FORMAT_PERCENTAGE: {
                        GString *new_str = g_string_new (NULL);
-                       gnm_render_general (NULL, new_str, go_format_measure_zero,
-                                           go_font_metrics_unit, f * 100,
-                                           -1, FALSE, 0, 0);
+                       gnm_float f100 = 100 * f;
+                       gboolean qsimple = (gnm_abs (f) < 100 && close_to_int (f100, 1e-6));
+                       gboolean qneg = (f < 0);
+                       gnm_render_general (NULL, new_str, go_format_measure_strlen,
+                                           go_font_metrics_unit, f100,
+                                           qsimple ? 12 + qneg: -1,
+                                           FALSE, 0, 0);
                        if (cursor_pos)
                                *cursor_pos = g_utf8_strlen (new_str->str, -1);
                        g_string_append_c (new_str, '%');


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]