Search code examples
wolfram-mathematica

Convert to floating number in Wolfram Mathematica?


Is there a quick and easy way to convert a floating number, in MATHEMATICA?

For example:

234 = 0.230*10^0
1.234 = 0.234*10^1
45.342 = 0.45342*10^2

then apply addition, subtraction, etc.

For example 234 + 12.5 in floating point it would be 0.234*10^0 + 0.125*10^1 applying rounding to 2 digits 0.23*10^0 + 0.13*10^1

I need a function that can convert any number to floating point

Thanks!


Solution

  • The following outputs its result as a string. This may be the only way to get the format you want, e.g. 0.45342*10^2 because otherwise Mathematica will convert it to 45.342.

    g[x_] := Module[{},
      p = Floor[Log[10, N[x]]] + 1;
      StringJoin[ToString[N[x/10^p]],
       "*10^", ToString[p]]]
    
    ans = g[45.342]
    

    0.45342*10^2

    Converting string to number.

    ToExpression[ans]
    

    45.342