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:03:46 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

tomas wrote:

>> It checks if object is number. That is why it is not usable
>> to check if string is actual number.
>
> D'oh, you are right. That'd been too easy ;-)
>
> It seems you'll have to go with a regexp, then do
> string-to-number.

One can use mine initial suggestion. It is a bit complicated -
not the code, but something about it ...

But the actual cause of the confusion is actually
`string-to-number' and this ambiguity:

  (string-to-number "not a number") ; 0
  (string-to-number "0")            ; 0

So ony should start by having string-to-number return nil,
not 0, when the input doesn't make sense!

(defun read-integer ()
  (let ((str)
        (str-number)
        (n) )
    (cl-loop until (progn
                     (setq str (read-string "integer: "))
                     (if (string= str "0")
                         (setq n 0)
                       (setq str-number (string-to-number str))
                       (unless (= str-number 0)
                         (setq n str-number) ))
                     (integerp n)) )
    n) )
;; (read-integer)

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




reply via email to

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