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.
This commit is contained in:
Mattias Engdegård 2021-04-27 17:36:15 +02:00
parent d55d5358b2
commit 7133a67dcd
2 changed files with 38 additions and 3 deletions

View 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))))

View 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))