aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2021-04-27 17:36:15 +0200
committerMattias EngdegÄrd2021-04-27 18:10:01 +0200
commit7133a67dcdb68fc16d71c3d45323baba8ac5afe9 (patch)
treeb8070a1471867514f86449ba96d7bca031c59141
parentd55d5358b27dee15ebbd998131d22b221f5f4964 (diff)
downloademacs-7133a67dcdb68fc16d71c3d45323baba8ac5afe9.tar.gz
emacs-7133a67dcdb68fc16d71c3d45323baba8ac5afe9.zip
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.
-rw-r--r--lisp/calc/calc.el14
-rw-r--r--test/lisp/calc/calc-tests.el27
2 files changed, 38 insertions, 3 deletions
diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el
index ec09abb34c4..1e7d5e7766c 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.")
483 "Floating-point numbers with this negative exponent or lower are displayed 483 "Floating-point numbers with this negative exponent or lower are displayed
484scientific notation in calc-mode.") 484scientific notation in calc-mode.")
485 485
486(defvar calc-digit-after-point nil
487 "If t, display at least one digit after the decimal point, as in `12.0'.
488If nil, the decimal point may come last in a number, as in `12.'.
489This setting only applies to floats in normal display mode.")
490
486(defvar calc-other-modes nil 491(defvar calc-other-modes nil
487 "List of used-defined strings to append to Calculator mode line.") 492 "List of used-defined strings to append to Calculator mode line.")
488 493
@@ -3184,7 +3189,8 @@ the United States."
3184 exp (- exp adj))))) 3189 exp (- exp adj)))))
3185 (setq str (int-to-string mant)) 3190 (setq str (int-to-string mant))
3186 (let* ((len (length str)) 3191 (let* ((len (length str))
3187 (dpos (+ exp len))) 3192 (dpos (+ exp len))
3193 (trailing-0 (and calc-digit-after-point "0")))
3188 (if (and (eq fmt 'float) 3194 (if (and (eq fmt 'float)
3189 (<= dpos (+ calc-internal-prec calc-display-sci-high)) 3195 (<= dpos (+ calc-internal-prec calc-display-sci-high))
3190 (>= dpos (+ calc-display-sci-low 2))) 3196 (>= dpos (+ calc-display-sci-low 2)))
@@ -3194,9 +3200,11 @@ the United States."
3194 (setq str (concat "0" point str))) 3200 (setq str (concat "0" point str)))
3195 ((and (<= exp 0) (> dpos 0)) 3201 ((and (<= exp 0) (> dpos 0))
3196 (setq str (concat (substring str 0 dpos) point 3202 (setq str (concat (substring str 0 dpos) point
3197 (substring str dpos)))) 3203 (substring str dpos)
3204 (and (>= dpos len) trailing-0))))
3198 ((> exp 0) 3205 ((> exp 0)
3199 (setq str (concat str (make-string exp ?0) point))) 3206 (setq str (concat str (make-string exp ?0)
3207 point trailing-0)))
3200 (t ; (< dpos 0) 3208 (t ; (< dpos 0)
3201 (setq str (concat "0" point 3209 (setq str (concat "0" point
3202 (make-string (- dpos) ?0) str)))) 3210 (make-string (- dpos) ?0) str))))
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el
index c5aa5a31eb2..13dd228d3b3 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."
191 (let ((calc-number-radix 36)) 191 (let ((calc-number-radix 36))
192 (should (equal (math-format-number 12345678901) "36#5,O6A,QT1"))))) 192 (should (equal (math-format-number 12345678901) "36#5,O6A,QT1")))))
193 193
194(ert-deftest calc-digit-after-point ()
195 "Test display of trailing 0 after decimal point (bug#47302)."
196 (let ((calc-digit-after-point nil))
197 ;; Integral floats have no digits after the decimal point (default).
198 (should (equal (math-format-number '(float 0 0)) "0."))
199 (should (equal (math-format-number '(float 5 0)) "5."))
200 (should (equal (math-format-number '(float 3 1)) "30."))
201 (should (equal (math-format-number '(float 23 0)) "23."))
202 (should (equal (math-format-number '(float 123 0)) "123."))
203 (should (equal (math-format-number '(float 1 -1)) "0.1"))
204 (should (equal (math-format-number '(float 54 -1)) "5.4"))
205 (should (equal (math-format-number '(float 1 -4)) "1e-4"))
206 (should (equal (math-format-number '(float 1 14)) "1e14"))
207 (should (equal (math-format-number 12) "12")))
208 (let ((calc-digit-after-point t))
209 ;; Integral floats have at least one digit after the decimal point.
210 (should (equal (math-format-number '(float 0 0)) "0.0"))
211 (should (equal (math-format-number '(float 5 0)) "5.0"))
212 (should (equal (math-format-number '(float 3 1)) "30.0"))
213 (should (equal (math-format-number '(float 23 0)) "23.0"))
214 (should (equal (math-format-number '(float 123 0)) "123.0"))
215 (should (equal (math-format-number '(float 1 -1)) "0.1"))
216 (should (equal (math-format-number '(float 54 -1)) "5.4"))
217 (should (equal (math-format-number '(float 1 -4)) "1e-4"))
218 (should (equal (math-format-number '(float 1 14)) "1e14"))
219 (should (equal (math-format-number 12) "12"))))
220
194(ert-deftest calc-calendar () 221(ert-deftest calc-calendar ()
195 "Test calendar conversions (bug#36822)." 222 "Test calendar conversions (bug#36822)."
196 (should (equal (calcFunc-julian (math-parse-date "2019-07-27")) 2458692)) 223 (should (equal (calcFunc-julian (math-parse-date "2019-07-27")) 2458692))