help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: How to read an integer from the minibuffer


From: Jean Louis
Subject: Re: How to read an integer from the minibuffer
Date: Fri, 12 Nov 2021 23:02:10 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

Improved check for number:

(defun string-is-number-p (s)
  "Return number only if string is actual number, otherwise NIL."
  (let* ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
          ((string-match "[^0123456789\\.-]" s) nil)
          ((string-match "-" s 1) nil)
          ((numberp (string-to-number s)) (string-to-number s)))))

(string-is-number-p "-0.1") ⇒ -0.1
(string-is-number-p "0.1") ⇒ 0.1
(string-is-number-p "0.1-") ⇒ nil
(string-is-number-p "  1 ") ⇒ 1
(string-is-number-p " a 1 ") ⇒ nil


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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