I want to be able to write a function which receives a number in scientific notation as a string and splits out of it the coefficient and the exponent as separate items. I could just use a regular expression, but the incoming number may not be normalised and I’d prefer to be able to normalise and then break the parts out.
A colleague has got part way of an solution using VB6 but it’s not quite there, as the transcript below shows.
cliVe> a = 1e6 cliVe> ? 'coeff: ' & o.spt(a) & ' exponent: ' & o.ept(a) coeff: 10 exponent: 5
should have been 1 and 6
cliVe> a = 1.1e6 cliVe> ? 'coeff: ' & o.spt(a) & ' exponent: ' & o.ept(a) coeff: 1.1 exponent: 6
correct
cliVe> a = 123345.6e-7 cliVe> ? 'coeff: ' & o.spt(a) & ' exponent: ' & o.ept(a) coeff: 1.233456 exponent: -2
correct
cliVe> a = -123345.6e-7 cliVe> ? 'coeff: ' & o.spt(a) & ' exponent: ' & o.ept(a) coeff: 1.233456 exponent: -2
should be -1.233456 and -2
cliVe> a = -123345.6e+7 cliVe> ? 'coeff: ' & o.spt(a) & ' exponent: ' & o.ept(a) coeff: 1.233456 exponent: 12
correct
Any ideas? By the way, Clive is a CLI based on VBScript and can be found on my weblog.
Google on "scientific notation regexp" shows a number of matches, including this one (don’t use it!!!!) which uses
which includes cases such as -.5e7 and +00000e33 (both of which you may not want to allow).
Instead, I would highly recommend you use the syntax on Doug Crockford’s JSON website which explicitly documents what constitutes a number in JSON. Here’s the corresponding syntax diagram taken from that page:
(source: json.org)
If you look at line 456 of his json2.js script (safe conversion to/from JSON in javascript), you’ll see this portion of a regexp:
which, ironically, doesn’t match his syntax diagram…. (looks like I should file a bug) I believe a regexp that does implement that syntax diagram is this one:
and if you want to allow an initial + as well, you get:
Add capturing parentheses to your liking.
I would also highly recommend you flesh out a bunch of test cases, to ensure you include those possibilities you want to include (or not include), such as:
Good luck!