Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica, convert string to number

I imported a table in mathematica using Import["..file","table"] now I cant use the numbers in the table as they are in string formta, can someone please explain how can i convert them from string to number again?

like image 632
user3745615 Avatar asked Jun 16 '14 16:06

user3745615


2 Answers

Another approach is to use Read. This is useful if you have numbers in "E" form scientific notation:

    Read[StringToStream[#], Number] &/@{ "1" ,"1.5" , "1E-20" , "2.E10" }

{1, 1.5, 1.5*10^-20, 2.*10^10}

Note ToExpression gets these wrong:

    ToExpression /@ {"1", "1.5", "1.5E-20", "2.E10"}

{1, 1.5, -15.9226, 2. E10}

"1.5E-20" is evaluated as 1.5 * 2.71828-20 in the last case the "E10" is taken as a new symbol..

ToExpression is however faster if you can use it..

like image 175
agentp Avatar answered Nov 18 '22 11:11

agentp


The intrinsic function ToExpression will convert its argument to an expression; if the argument is the string representation of a number the function will return the number.

like image 29
High Performance Mark Avatar answered Nov 18 '22 11:11

High Performance Mark