]> git.eshelyaron.com Git - emacs.git/commitdiff
Calc: control digits after decimal point (bug#47302)
authorMattias Engdegård <mattiase@acm.org>
Tue, 27 Apr 2021 15:36:15 +0000 (17:36 +0200)
committerMattias Engdegård <mattiase@acm.org>
Tue, 27 Apr 2021 16:10:01 +0000 (18:10 +0200)
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
test/lisp/calc/calc-tests.el

index ec09abb34c4193ce740ea7bfc36c1efce5f0fc59..1e7d5e7766c885e47ff91eee7dbce5ad4fe80332 100644 (file)
@@ -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))))
index c5aa5a31eb20bf040491adaa49164e0c611528e4..13dd228d3b3cab2b5659b52b6e88abc4381faaf4 100644 (file)
@@ -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))