This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

How do I find the length of a number?

4 posts · 4333 views

is there anyway to find the length of a number e.g.
 (length 1234567890) 
would equal 10, or:
(length 5487)
would equal 4

Re: How do I find the length of a number?

Mathematical way:
(defun number-length (num)
  (if (zerop num)
      1
      (1+ (floor (log num 10)))))
or text-representation-way:
(defun number-length (num)
  (length (format nil "~s" num)))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: How do I find the length of a number?

Thanks a lot! That is just what I was looking for. I really do appreciate all these great answers you have given me.

Re: How do I find the length of a number?

Define "length of a number". For INTEGERs it is one thing, for floating point numbers not so easy.

In any case, you have INTEGER-LENGTH in ANSI, which may or may not be of use.
Marco Antoniotti