# Convert Fortran-like number form to traditional number form

What is the most elegant way to convert arbitrary numbers written as (verbatim):

1.13903 e - 08


into traditional Mathematica number form without having to convert the number into text (string), performing text substitutions, and so on?

(Of course 1.13903 e - 08 is $$1.13903 \cdot 10^{-8}$$.)

Closest to sensible I can imagine would be wrapping these numbers individually with a function if e is always followed by sign (+or -):

ClearAll@fn;
fn[m_. e + e_] := m 10^e;
fn[m_. e] := m;
fn[_] := 0;

fn[1.13903 e - 08]


1.13903*10^-8

Handling the case where + is implicit seems awfully convoluted to get right.

I also thought of redefining e as an operator, but firstly I don't think arbitrary letters can be redefined that way, and secondly all hell would probably break loose if such a definition would be present globally.

• Yes... this works, and certainly along my initial efforts. Thanks. I was hoping for some clever option for NumberForm but this will do. ($\checkmark$) May 7 '20 at 5:50
• But it actually works in this form only if your have + or - after e! I'll try to work a little more on it. May 7 '20 at 5:52
• HoldAll will help you handling 4 e 5 :) . May 7 '20 at 6:53
• @xzczd I tried it and failed to make it work in a sensible manner. Probably worth a second answer. In particular, ordering changes seem to occur depending on values of mantissa and exponent. May 7 '20 at 6:55
• That should not happen. Orderless doesn't start up in a function with HoldAll attribute. See my answer for more info. May 7 '20 at 7:04

An enhanced version of kirma's answer, which handles expressions like 5 e 4:

ClearAll@fn;
SetAttributes[fn, HoldAll]
fn[m_. e + e_] := m 10^e
fn[a_. e b_] := a 10^b

fn[5 e 4]
(* 50000 *)

• Hah! I tested my function... having wrong definition overriding the one I was attempting to test in the front of tests being run. Now I understand why it didn't work. :) May 7 '20 at 7:06

you are fighting with windmills.

   Interpreter["Number"]["4.5E-7"]


gives you 4.5*10^-7

• But OP mentions that he doesn't want to "convert the number into text (string)". And for that case, I'll use InternalStringToDouble. May 7 '20 at 7:57
• @xzczd, How can you import arbitrary number to MMa? I know two ways - read from file and from shared memory. Both of them mean interpretation of textual or binary data. However, the form like xxE-yy is direct pointer to the textual data isn't it? May 7 '20 at 8:02
• One possibility is, OP just want to type numbers using the e notation. Similar question comes up before: mathematica.stackexchange.com/q/135867/1871 May 7 '20 at 8:08
• But Interpreter["Number"]["1.13903 e - 08"] returns a Failure. May 7 '20 at 8:28
• @DavidG.Stork: Even then, the most practical solution is probably Interpreter["Number"][StringDelete["1.13903 e - 08", " "]]`, even if it does technically "perform a text substitution". May 8 '20 at 12:39