emacs-diffs
[Top][All Lists]
Advanced

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

master 7133a67: Calc: control digits after decimal point (bug#47302)


From: Mattias Engdegård
Subject: master 7133a67: Calc: control digits after decimal point (bug#47302)
Date: Tue, 27 Apr 2021 12:15:42 -0400 (EDT)

branch: master
commit 7133a67dcdb68fc16d71c3d45323baba8ac5afe9
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    Calc: control digits after decimal point (bug#47302)
    
    Calc normally displays a trailing decimal point for floats with no
    fractional part, like '12.'. Some uses require at least one digit
    after the point; add the governing variable calc-digit-after-point.
    
    * lisp/calc/calc.el (calc-digit-after-point): New variable.
    (math-format-number): Use it.
    * test/lisp/calc/calc-tests.el (calc-display-digit-after-point):
    New test.
---
 lisp/calc/calc.el            | 14 +++++++++++---
 test/lisp/calc/calc-tests.el | 27 +++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el
index ec09abb..1e7d5e7 100644
--- a/lisp/calc/calc.el
+++ b/lisp/calc/calc.el
@@ -483,6 +483,11 @@ current precision are displayed in scientific notation in 
calc-mode.")
   "Floating-point numbers with this negative exponent or lower are displayed
 scientific notation in calc-mode.")
 
+(defvar calc-digit-after-point nil
+  "If t, display at least one digit after the decimal point, as in `12.0'.
+If nil, the decimal point may come last in a number, as in `12.'.
+This setting only applies to floats in normal display mode.")
+
 (defvar calc-other-modes nil
   "List of used-defined strings to append to Calculator mode line.")
 
@@ -3184,7 +3189,8 @@ the United States."
                      exp (- exp adj)))))
          (setq str (int-to-string mant))
          (let* ((len (length str))
-                (dpos (+ exp len)))
+                (dpos (+ exp len))
+                 (trailing-0 (and calc-digit-after-point "0")))
            (if (and (eq fmt 'float)
                     (<= dpos (+ calc-internal-prec calc-display-sci-high))
                     (>= dpos (+ calc-display-sci-low 2)))
@@ -3194,9 +3200,11 @@ the United States."
                    (setq str (concat "0" point str)))
                   ((and (<= exp 0) (> dpos 0))
                    (setq str (concat (substring str 0 dpos) point
-                                     (substring str dpos))))
+                                     (substring str dpos)
+                                      (and (>= dpos len) trailing-0))))
                   ((> exp 0)
-                   (setq str (concat str (make-string exp ?0) point)))
+                   (setq str (concat str (make-string exp ?0)
+                                      point trailing-0)))
                   (t   ; (< dpos 0)
                    (setq str (concat "0" point
                                      (make-string (- dpos) ?0) str))))
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el
index c5aa5a3..13dd228 100644
--- a/test/lisp/calc/calc-tests.el
+++ b/test/lisp/calc/calc-tests.el
@@ -191,6 +191,33 @@ An existing calc stack is reused, otherwise a new one is 
created."
     (let ((calc-number-radix 36))
       (should (equal (math-format-number 12345678901) "36#5,O6A,QT1")))))
 
+(ert-deftest calc-digit-after-point ()
+  "Test display of trailing 0 after decimal point (bug#47302)."
+  (let ((calc-digit-after-point nil))
+    ;; Integral floats have no digits after the decimal point (default).
+    (should (equal (math-format-number '(float 0 0)) "0."))
+    (should (equal (math-format-number '(float 5 0)) "5."))
+    (should (equal (math-format-number '(float 3 1)) "30."))
+    (should (equal (math-format-number '(float 23 0)) "23."))
+    (should (equal (math-format-number '(float 123 0)) "123."))
+    (should (equal (math-format-number '(float 1 -1)) "0.1"))
+    (should (equal (math-format-number '(float 54 -1)) "5.4"))
+    (should (equal (math-format-number '(float 1 -4)) "1e-4"))
+    (should (equal (math-format-number '(float 1 14)) "1e14"))
+    (should (equal (math-format-number 12) "12")))
+  (let ((calc-digit-after-point t))
+    ;; Integral floats have at least one digit after the decimal point.
+    (should (equal (math-format-number '(float 0 0)) "0.0"))
+    (should (equal (math-format-number '(float 5 0)) "5.0"))
+    (should (equal (math-format-number '(float 3 1)) "30.0"))
+    (should (equal (math-format-number '(float 23 0)) "23.0"))
+    (should (equal (math-format-number '(float 123 0)) "123.0"))
+    (should (equal (math-format-number '(float 1 -1)) "0.1"))
+    (should (equal (math-format-number '(float 54 -1)) "5.4"))
+    (should (equal (math-format-number '(float 1 -4)) "1e-4"))
+    (should (equal (math-format-number '(float 1 14)) "1e14"))
+    (should (equal (math-format-number 12) "12"))))
+
 (ert-deftest calc-calendar ()
   "Test calendar conversions (bug#36822)."
   (should (equal (calcFunc-julian (math-parse-date "2019-07-27")) 2458692))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]