Converting Strings to Real Numbers

Converting Strings to Real Numbers

kgrupenhof
Participant Participant
4,287 Views
3 Replies
Message 1 of 4

Converting Strings to Real Numbers

kgrupenhof
Participant
Participant

I'm having some issues dealing with data I have received from an Excel file.  I'm using a routine I found called GetExcel which pulls data from a spreadsheet and stores them as a list.  My issue is that the list doesn't contain numbers (although Excel does) so I cannot use them like I need to.

 

Currently I have the following when I ask to print my list (called tslst).

 

tslst
(("0") ("250") ("500") ("750") ("1000") ("1250"))

 

From what I can tell, these are not numbers.  How can I convert them?  I've tried atof.

0 Likes
Accepted solutions (2)
4,288 Views
3 Replies
Replies (3)
Message 2 of 4

Ranjit_Singh
Advisor
Advisor
Accepted solution

@kgrupenhof wrote:

........

tslst
(("0") ("250") ("500") ("750") ("1000") ("1250"))

From what I can tell, these are not numbers.  How can I convert them?  I've tried atof.


That list you can simply convert using

(mapcar '(lambda (x) (atof (car x)))) tslst)
Command: (mapcar '(lambda (x) (atof (car x))) tslst)
(0.0 250.0 500.0 750.0 1000.0 1250.0)

 

But if the data in excel was real type data and during import it was converted to string type then the decimal portion maybe lost. You may want to share your excel import code.

 

0 Likes
Message 3 of 4

Kent1Cooper
Consultant
Consultant
Accepted solution

They're not just text strings, but little lists, each of which contains one text string.  Do you need them turned into one list of just numbers?

 

Command: (mapcar 'atoi (mapcar 'car tslst))
(0 250 500 750 1000 1250)

 

or, for real numbers:

 

Command: (mapcar 'atof (mapcar 'car tslst))
(0.0 250.0 500.0 750.0 1000.0 1250.0)

 

Or do you need the numbers still held inside little lists within the overall list?  That's also doable:

 

Command: (mapcar '(lambda (x) (list (atoi (car x)))) tslst)
((0) (250) (500) (750) (1000) (1250))

 

or

 

Command: (mapcar '(lambda (x) (list (atof (car x)))) tslst)
((0.0) (250.0) (500.0) (750.0) (1000.0) (1250.0))

Kent Cooper, AIA
0 Likes
Message 4 of 4

kgrupenhof
Participant
Participant

Thank you both.  That solved it.  Greatly appreciated.

0 Likes