6
$\begingroup$

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

$\endgroup$

3 Answers 3

7
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ Yes... this works, and certainly along my initial efforts. Thanks. I was hoping for some clever option for NumberForm but this will do. ($\checkmark$) $\endgroup$ Commented May 7, 2020 at 5:50
  • 1
    $\begingroup$ But it actually works in this form only if your have + or - after e! I'll try to work a little more on it. $\endgroup$
    – kirma
    Commented May 7, 2020 at 5:52
  • $\begingroup$ HoldAll will help you handling 4 e 5 :) . $\endgroup$
    – xzczd
    Commented May 7, 2020 at 6:53
  • $\begingroup$ @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. $\endgroup$
    – kirma
    Commented May 7, 2020 at 6:55
  • $\begingroup$ That should not happen. Orderless doesn't start up in a function with HoldAll attribute. See my answer for more info. $\endgroup$
    – xzczd
    Commented May 7, 2020 at 7:04
7
$\begingroup$

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 *)
$\endgroup$
1
  • 1
    $\begingroup$ 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. :) $\endgroup$
    – kirma
    Commented May 7, 2020 at 7:06
3
$\begingroup$

you are fighting with windmills.

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

gives you 4.5*10^-7

$\endgroup$
7
  • 2
    $\begingroup$ But OP mentions that he doesn't want to "convert the number into text (string)". And for that case, I'll use Internal`StringToDouble. $\endgroup$
    – xzczd
    Commented May 7, 2020 at 7:57
  • 1
    $\begingroup$ @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? $\endgroup$
    – Rom38
    Commented May 7, 2020 at 8:02
  • $\begingroup$ One possibility is, OP just want to type numbers using the e notation. Similar question comes up before: mathematica.stackexchange.com/q/135867/1871 $\endgroup$
    – xzczd
    Commented May 7, 2020 at 8:08
  • 1
    $\begingroup$ But Interpreter["Number"]["1.13903 e - 08"] returns a Failure. $\endgroup$
    – corey979
    Commented May 7, 2020 at 8:28
  • 2
    $\begingroup$ @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". $\endgroup$ Commented May 8, 2020 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.