Suppose I have a string containing the C-representation of a floating point number, for example
s = "1.23e-5"
and I want to convert this to a Mathematica number. How can I do this?
ToExpression[s] gives Plus[-5, Times[1.23`, e]].
|
Suppose I have a string containing the C-representation of a floating point number, for example
and I want to convert this to a Mathematica number. How can I do this?
|
||||
|
|
I think probably the cleanest way to do this (at least, if you have only a single string, or are faced with a separate string for each number you wish to convert as a result of some other process) is to use the undocumented function
which gives:
However, if you are trying to convert many such numbers at once, the standard, documented methods ( |
|||||||||||||||||||
|
Which is not as good as what you started with. Note that it is important to close the stream. Szabolcs says this is difficult to read. That was surely not my intention. You could also write it verbosely like this:
|
||||
|
|
|
|
|||||||||||
|
|
May be one can try the following
|
|||||||||
|
|
On version 7
This sent me looking for another way to convert numeric strings. Using Being an internal function is it not error tolerant and if fed bad arguments it will crash the kernel. The syntax is as follows:
table : table of strings, depth = 2; need not be rectangular. pre : List of literal strings to ignore if preceding the digits (only first match tried). post : List of literal strings to ignore if following the digits (only first match tried). neg : literal string to interpret a negative sign (`-`). pos : literal string to interpret a positive sign (`+`). dot : literal string to interpret as decimal point. (Using Example:
|
|||||||||
|