6

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}$.)

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
David G. Stork
  • 41,180
  • 3
  • 34
  • 96

3 Answers3

7

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.

kirma
  • 19,056
  • 1
  • 51
  • 93
  • 1
    Yes... this works, and certainly along my initial efforts. Thanks. I was hoping for some clever option for NumberForm but this will do. ($\checkmark$) – David G. Stork May 07 '20 at 05:50
  • 1
    But it actually works in this form only if your have + or - after e! I'll try to work a little more on it. – kirma May 07 '20 at 05:52
  • HoldAll will help you handling 4 e 5 :) . – xzczd May 07 '20 at 06: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. – kirma May 07 '20 at 06:55
  • That should not happen. Orderless doesn't start up in a function with HoldAll attribute. See my answer for more info. – xzczd May 07 '20 at 07:04
  • @xzczd Stupid trouble on my code, as I stated on comment to your answer. – kirma May 07 '20 at 07:08
7

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 *)
xzczd
  • 65,995
  • 9
  • 163
  • 468
  • 1
    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. :) – kirma May 07 '20 at 07:06
3

you are fighting with windmills.

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

gives you 4.5*10^-7

Rom38
  • 5,129
  • 13
  • 28
  • 2
    But OP mentions that he doesn't want to "convert the number into text (string)". And for that case, I'll use [Internal\StringToDouble`](https://mathematica.stackexchange.com/a/1744/1871). – xzczd May 07 '20 at 07:57
  • 1
    @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? – Rom38 May 07 '20 at 08:02
  • One possibility is, OP just want to type numbers using the e notation. Similar question comes up before: https://mathematica.stackexchange.com/q/135867/1871 – xzczd May 07 '20 at 08:08
  • 1
    But Interpreter["Number"]["1.13903 e - 08"] returns a Failure. – corey979 May 07 '20 at 08:28
  • @corey979, I guess due to the spaces – Rom38 May 07 '20 at 10:56
  • @Rom38: It is a clever idea, but alas doesn't work for my case, as corey979 points out. – David G. Stork May 07 '20 at 18:10
  • 2
    @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". – Ilmari Karonen May 08 '20 at 12:39