From: Ralph Wesser
Subject: Convert an integer to string or list of digits?
Date: 
Message-ID: <sbts7l9pee6104@corp.supernews.com>
I,m new to Lisp (reading chapt 10 in Winston & Horne's book "LISP") and I'm
looking for a way to take an integer  such as 123 and either convert it to a
string "123" or a list (1 2 3) of its digits.  Most of my recent experience
is in C/C++ and a Lisp version of the function "itoa" would be ideal.  Is
there anything like it.

From: Barry Margolin
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <%MDv4.40$NC6.943@burlma1-snr2>
In article <··············@corp.supernews.com>,
Ralph Wesser <·······@alaska.net> wrote:
>I,m new to Lisp (reading chapt 10 in Winston & Horne's book "LISP") and I'm
>looking for a way to take an integer  such as 123 and either convert it to a
>string "123" or a list (1 2 3) of its digits.  Most of my recent experience
>is in C/C++ and a Lisp version of the function "itoa" would be ideal.  Is
>there anything like it.

FORMAT is to Common Lisp as printf/sprintf is to C.

(format nil "~d" 123) => "123"

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Christopher C Stacy
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <x8lwvnk258j.fsf@world.std.com>
>>>>> On Thu, 2 Mar 2000 13:55:38 -0900, Ralph Wesser ("Ralph") writes:

 Ralph> I,m new to Lisp (reading chapt 10 in Winston & Horne's book "LISP") and I'm
 Ralph> looking for a way to take an integer  such as 123 and either convert it to a
 Ralph> string "123" or a list (1 2 3) of its digits.  Most of my recent experience
 Ralph> is in C/C++ and a Lisp version of the function "itoa" would be ideal.  Is
 Ralph> there anything like it.

I can imagine applications where you might want to create a string:

  (format nil "~D" 123)  =>  "123"

or back the other way:

  (parse-integer "123")  => 123

But I am not sure why you would be interested in turning an integer
into a sequence of numbers; that seems like an arcane thing to do.

I would probably go about it by first parsing the number into a string,
as above.  That string is a SEQUENCE data type, so I could use the SEQUENCE
functions on each of the CHARACTER digits without any further work.  
If list structure was necessary, you could do this:

  (map 'list #'digit-char-p "123")   => (1 2 3)


cheers,
Chris
From: Ralph Wesser
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <sbvop4e6ee656@corp.supernews.com>
I agree that converting an integer to a string or list of digits does seem a
little strange.  How I ended up wanting to do this is that I'm trying to do
one of my daughter's C++ computer science assignments by using Lisp.  I
found her assignment interesting and it allows me to try something more
involved with Lisp.  So far, I'm impressed by the language.  It has a
concise and elegant feel, and I enjoy using it - to the neglect of the C++
tasks I need to be doing.  Thanks for your and Barry's help.
From: Akyar (MIP Ltd)
Subject: Ho to convert sting to a float ??
Date: 
Message-ID: <38C00174.4F74D57F@mip.hu>
Hi,

... maybe you can also help me to convert a string to a floating number
?

e.g  "123.23" --> 123.23 

Thanks in advance 


Sinan





Christopher C Stacy wrote:
> 
> >>>>> On Thu, 2 Mar 2000 13:55:38 -0900, Ralph Wesser ("Ralph") writes:
> 
>  Ralph> I,m new to Lisp (reading chapt 10 in Winston & Horne's book "LISP") and I'm
>  Ralph> looking for a way to take an integer  such as 123 and either convert it to a
>  Ralph> string "123" or a list (1 2 3) of its digits.  Most of my recent experience
>  Ralph> is in C/C++ and a Lisp version of the function "itoa" would be ideal.  Is
>  Ralph> there anything like it.
> 
> I can imagine applications where you might want to create a string:
> 
>   (format nil "~D" 123)  =>  "123"
> 
> or back the other way:
> 
>   (parse-integer "123")  => 123
> 
> But I am not sure why you would be interested in turning an integer
> into a sequence of numbers; that seems like an arcane thing to do.
> 
> I would probably go about it by first parsing the number into a string,
> as above.  That string is a SEQUENCE data type, so I could use the SEQUENCE
> functions on each of the CHARACTER digits without any further work.
> If list structure was necessary, you could do this:
> 
>   (map 'list #'digit-char-p "123")   => (1 2 3)
> 
> cheers,
> Chris

-- 

With my kind regards,
Mehmet Akyar 
MIP

******************************************************************

MIP LTD
V�r�smarty u. 53/2
1064 Budapest 
HUNGARY

Tel:   ++36 1 269 52 50
Fax:   ++36 1 269 52 51

Email:   ·····@mip.hu

******************************************************************
From: Rudolf Schlatte
Subject: Re: Ho to convert sting to a float ??
Date: 
Message-ID: <lxbt4uewhj.fsf@ist.tu-graz.ac.at>
"Akyar (MIP Ltd)" <·····@mip.hu> writes:

> Hi,
> 
> ... maybe you can also help me to convert a string to a floating number
> ?
> 
> e.g  "123.23" --> 123.23 
> 
> Thanks in advance 
> 

CL-USER[2]> (read-from-string "123.45")
123.45 ;
6
CL-USER[9]> (type-of (read-from-string "123.45"))
SINGLE-FLOAT
CL-USER[10]> 

Regards,

Rudi
From: Joe Marshall
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <un1ogp0vc.fsf@alum.mit.edu>
 >>>>> On Thu, 2 Mar 2000 13:55:38 -0900, Ralph Wesser ("Ralph") writes:
 
  Ralph> I,m new to Lisp (reading chapt 10 in Winston & Horne's book "LISP") and I'm
  Ralph> looking for a way to take an integer  such as 123 and either convert it to a
  Ralph> string "123" or a list (1 2 3) of its digits.  Most of my recent experience
  Ralph> is in C/C++ and a Lisp version of the function "itoa" would be ideal.  Is
  Ralph> there anything like it.
 
The ROUND function divides two integers and returns two integer
values:   the quotient and the remainder.  So (ROUND 123 10) will
return 12 and 3.  You can use this to peel off individual digits from
an integer.  Mix this with some looping or recursion and you've got
it.

~jrm
From: Coby Beck
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <952110023070@NewsSIEVE.cs.bonn.edu>
Joe Marshall <·········@alum.mit.edu> wrote in message
··················@alum.mit.edu...
> >>>>> On Thu, 2 Mar 2000 13:55:38 -0900, Ralph Wesser ("Ralph") writes:
>
>   Ralph> I,m new to Lisp (reading chapt 10 in Winston & Horne's book
"LISP") and I'm
>   Ralph> looking for a way to take an integer  such as 123 and either
convert it to a
>   Ralph> string "123" or a list (1 2 3) of its digits.  Most of my recent
experience
>   Ralph> is in C/C++ and a Lisp version of the function "itoa" would be
ideal.  Is
>   Ralph> there anything like it.
>
> The ROUND function divides two integers and returns two integer
> values:   the quotient and the remainder.  So (ROUND 123 10) will
> return 12 and 3.  You can use this to peel off individual digits from
> an integer.  Mix this with some looping or recursion and you've got
> it.
>

I think you are looking for truncate, not round
-> (round 123 10)
12
3
-> (round 456 10)
46
-4
-> (truncate 123 10)
12
3
-> (truncate 456 10)
45
6
->
here's a looping solution:

(defun list-digits(num)
  (when (integerp num)
    (let (digit)
      (reverse (loop while (> num 0) do
                     (multiple-value-setq (num next-digit) (truncate num
10))
                   collect next-digit)))))
-> (list-digits 3456789)
(3 4 5 6 7 8 9)
->

coby
From: Joe Marshall
Subject: Re: Convert an integer to string or list of digits?
Date: 
Message-ID: <uhfemps45.fsf@alum.mit.edu>
"Coby Beck" <·····@mercury.bc.ca> writes:

> Joe Marshall <·········@alum.mit.edu> wrote in message
> >
> > The ROUND function divides two integers and returns two integer
> > values:   the quotient and the remainder.  So (ROUND 123 10) will
> > return 12 and 3.  You can use this to peel off individual digits from
> > an integer.  Mix this with some looping or recursion and you've got
> > it.
> >
> 
> I think you are looking for truncate, not round
> -> (round 123 10)
> 12
> 3

Yes, you want truncate.  My mistake.

--
~jrm