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: Emanuel Berg
Subject: Re: How to read an integer from the minibuffer
Date: Tue, 16 Nov 2021 07:39:52 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Jean Louis wrote:

>> A form control for integer numbers only permits typing
>> digits (as you initially asked). Now the user cannot enter
>> -42.
>
> Integers may be negative.

It is better to make _one_ "string-to-number" that makes
sense, then one can easily add extra checks with `integerp'
and `(< 0 n)' and whatever to make it more specific from there...

Here is a start ...

(defun string-to-number-number (str)
  (if (string= str "0")
      0
    (let ((num (string-to-number str)))
      (unless (zerop num)
        num) )))
;; (string-to-number-number "10")
;; (string-to-number-number "1.5")
;; (string-to-number-number "0")
;; (string-to-number-number "-1.5")
;; (string-to-number-number "-10")
;; (string-to-number-number "not a number") ; nil

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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