3
\$\begingroup\$

Near the middle of the semester in chemistry, we get problems like:

Convert 1.283850057 to Scientific Notation

and

Convert 3.583✕106 to Standard Notation

This is just stupid busy work for people who already know how to do this. So…

The Challenge

Given an input in any reasonable, convenient format, in either standard notation or scientific notation, output the same number in the other notation. However, note that e will not be accepted in place of *10^, as this gives a significant advantage to languages with builtins with e.

If not otherwise specified, standard notation will take this format:

x             (an integer)
xxxxxxxxxx    (a large integer with no more than 10 digits)
x.xxxx        (a float)
x.xxxxxxxxxx  (a float with no more than 10 x's after the decimal point)

While scientific notation will take this format:

x*10^x
x.zzzz*10^z (x will be >= 1)
x*10^-x
x.zz*10^-xx (x will be >= 1)

I/O

Input will never contain more than 10 leading+10 trailing digits, and you may decide how to deal with trailing zeroes after the decimal point. The exponent will never be so large as to max out your language's allowance for large numbers. You should only need to handle up to about 1010.

input => output

---scientific to standard---

4.5*10^7 => 45000000
1*10^0 => 1
8.9*10^-2 => 0.089
1.0001*10^5 => 100010
9.999999*10^9 => 9999999000
4.00038*10^2 => 400.038

---standard to scientific---

7354 = 7.354*10^3
385.43 => 3.8543*10^2
0.32 = 3.2*10^-1
0.00044 => 4.4*10^-4
0.053 => 5.3*10^-2

As always, standard loopholes are disallowed. This is , so shortest bytes wins!

\$\endgroup\$
  • \$\begingroup\$ @Uriel that is true and yes, this isn't an extremely hard challenge (litotes), but it also involves doing the opposite conversion, which adds a degree of complexity \$\endgroup\$ – FantaC Dec 11 '17 at 20:55
  • \$\begingroup\$ I realize I answered already, but do we have to use *10^ for e when outputting scientific notation? \$\endgroup\$ – Giuseppe Dec 11 '17 at 21:14
  • 1
    \$\begingroup\$ @Giuseppe Yessir \$\endgroup\$ – FantaC Dec 11 '17 at 21:55
4
\$\begingroup\$

R, 70 bytes

function(s)sub("e","*10^",format(eval(parse(t=s)),sc=!grepl("\\^",s)))

Try it online!

An anonymous function. Takes input as a string and outputs as a string.

This function evaluates the string eval(parse(t=s)), then formats it, with the flag scientific set to whether the string contains a ^ or not. Then it replaces e with *10^ to match the right output spec.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 100 bytes

lambda a:eval(a.replace("*10^","e+"))if type(a)==str else re.sub("0*e\+?0*","*10^","%e"%a)
import re

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 83 80 bytes

a=>(r=a.split(N='*10^'))[1]?+r.join`e`+'':(+a).toExponential().replace(/e\+?/,N)

An anonymous function. Takes input as a string and outputs as a string.

\$\endgroup\$
  • \$\begingroup\$ Hacked on '1.3*10^-8' \$\endgroup\$ – l4m2 Dec 13 '17 at 17:31
1
\$\begingroup\$

Python 2, 87 bytes

lambda s:eval(s.replace('^','**'))if'^'in`s`else re.sub('e\+?','*10^','%e'%s)
import re

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 80 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] and outputs to the VBE immediate window.

v=Evaluate("="&[A1]):e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v*10^-e &"*10^"&e)

''  Or

v=Evaluate("="&[A1]):e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v/(10^e)&"*10^"&e)

''  Or

[B1]="="&[A1]:v=[B1]:e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v*10^-e &"*10^"&e)

''  Or

[B1]="="&[A1]:e=[Int(Log10(B1))]:?IIf(Instr([A1],"^"),[B1],[B1]/(10^e)&"*10^"&e)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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