[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to read an integer from the minibuffer
From: |
Yuri Khan |
Subject: |
Re: How to read an integer from the minibuffer |
Date: |
Tue, 16 Nov 2021 18:18:31 +0700 |
On Tue, 16 Nov 2021 at 16:38, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> (defun string-to-number-number (str)
> (let ((s (string-trim str)))
> (if (string-match "^[+-]\\{0,1\\}\\(0+\\|0*\\.0+\\)$" s)
> 0
> (let ((num (string-to-number s)))
> (when (and (not (zerop num))
> (string-match
> "^[+-]\\{0,1\\}\\([[:digit:]]+\\|[[:digit:]]*\\.[[:digit:]]+\\)$" s) )
> num) ))))
If you’re checking the input string by regexp anyway, you could do
that before calling the parser and simplify to:
(defun string-to-number-strict (str)
(if (string-match
"^[+-]?\\([[:digit:]]+\\|[[:digit:]]*\\.[[:digit:]]+\\)$" s)
(string-to-number s)
(error "invalid number")))
(Depending on circumstances, you might want to refine the regexp to
disallow leading zeros unless it is the only digit in the integer
part; and/or allow exponential format. It might also be a good idea to
wrap all that in a ‘save-match-data’.)
Re: How to read an integer from the minibuffer, Marcin Borkowski, 2021/11/12
- Re: How to read an integer from the minibuffer, Emanuel Berg, 2021/11/16
- Re: How to read an integer from the minibuffer, Yuri Khan, 2021/11/16
- Re: How to read an integer from the minibuffer, Emanuel Berg, 2021/11/16
- Re: How to read an integer from the minibuffer,
Yuri Khan <=
- Re: How to read an integer from the minibuffer, Emanuel Berg, 2021/11/16
- Re: How to read an integer from the minibuffer, Yuri Khan, 2021/11/16
- Re: How to read an integer from the minibuffer, Emanuel Berg, 2021/11/16
- Re: How to read an integer from the minibuffer, Emanuel Berg, 2021/11/16