File Exchange

## Scientific Prefix to Number

version 3.1.2 (14.3 KB) by Stephen Cobeldick

### Stephen Cobeldick (view profile)

Convert a metric prefixed string into numeric value (SI/engineering). Bonus: binary prefixes!

Updated 21 Feb 2020

The function SIP2NUM converts a string with a metric prefix (aka SI prefix, or engineering prefix) into numeric values. For example the string '1 k' is converted to 1000. The bonus function BIP2NUM converts from Binary-prefixed string to numeric, for example the value '1 Ki' is converted to 1024.

After testing the metric-prefix submissions on MATLAB FEX (see Acknowledgements) and not finding a single one that converted all values correctly, I wrote my own functions. And then exhaustively tested them to confirm that they actually give the correct output.

This submission:
* Automatically detects the prefix, or it may be specified as being either the name or symbol.
* Detects coefficients including +/- sign, decimal digits and exponent E-notation.
* Detects zero or more coefficients in the string.
* Returns the parts of the input string that are split by the detected coefficients & prefixes.
* Returns the number of significant figures detected in the coefficients.

### Reverse Conversion ###

To convert from numeric to metric-prefixed string:

### Metric Prefix Examples ###

>> sip2num('10 k') OR sip2num('10.0 kilo') OR sip2num('10000') OR sip2num('1e4')
ans = 10000

>> [val,spl] = sip2num('Power: 200 megawatt')
val = 200000000
spl = {'Power: ','watt'}

>> [val,spl,sgf] = sip2num('from -3.6 MV to +1.24kV potential difference.')
val = [-3600000,1240]
spl = {'from ','V to ','V potential difference.'}
sgf = [2,3]

>> [val,spl] = sip2num('100 meter','meter') % Try it without the second option.
val = 100
spl = {'','meter'}

>> sip2num(num2sip(9e12)) % 9 tera == 9e12 == 9*1000^4
ans = 9000000000000

### Binary Prefix Examples ###

>> bip2num('10 Ki') OR bip2num('10.0 kibi') OR bip2num('10240') OR bip2num('1.024e4')
ans = 10240

>> [val,spl] = bip2num('Memory: 200 mebibyte')
val = 209715200
spl = {'Memory: ','byte'}

>> [val,spl,sgf] = bip2num('From -3.6 MiB to +1.24KiB data allowance.')
val = [-3774873.6,1269.76]
spl = {'From ','B to ','B data allowance.'}
sgf = [2,3]

>> [val,spl] = bip2num('100 Pixel','Pixel') % Try it without the second option.
val = 100
spl = {'','Pixel'}

>> bip2num(num2bip(pow2(9,40))) % 9 tebi == pow2(9,40) == 9*1024^4
ans = 9895604649984

### Metric Prefixes ###

Magnitude | Symbol / Name
1000^-8 | y / yocto
1000^-7 | z / zepto
1000^-6 | a / atto
1000^-5 | f / femto
1000^-4 | p / pico
1000^-3 | n / nano
1000^-2 | µ / micro
1000^-1 | m / milli
1000^0 |
1000^1 | k / kilo
1000^2 | M / mega
1000^3 | G / giga
1000^4 | T / tera
1000^5 | P / peta
1000^6 | E / exa
1000^7 | Z / zetta
1000^8 | Y / yotta

### Binary Prefixes (IEC 60027-2 A.2 and ISO/IEC 80000-13:2008) ###

Magnitude | Symbol / Name
1024^0 |
1024^1 | Ki / kibi
1024^2 | Mi / mebi
1024^3 | Gi / gibi
1024^4 | Ti / tebi
1024^5 | Pi / pebi
1024^6 | Ei / exbi
1024^7 | Zi / zebi
1024^8 | Yi / yobi

### Notes ###

These functions have been extensively tested against many edge cases, with particular attention to ensuring the correct handling of exponential notation. Compared to similar submissions available on MATLAB File Exchange, these functions correctly:
* parse negative strings (try '-1').
* parse any combination of E-notation values (try '1e0', '1e0 k', '1e30').
* accept different "micro" symbol characters: U+0075, or U+00B5, or U+03BC,

### Cite As

Stephen Cobeldick (2020). Scientific Prefix to Number (https://www.mathworks.com/matlabcentral/fileexchange/53886-scientific-prefix-to-number), MATLAB Central File Exchange. Retrieved .

HarryDymond

### HarryDymond (view profile)

Thanks for taking my suggestion on board and implementing the change so quickly :-)

Stephen Cobeldick

### Stephen Cobeldick (view profile)

@HarryDymond: that is a very good idea. I just added support for the characters you specified, can you please test it again.

HarryDymond

### HarryDymond (view profile)

Please could you add support for the micro symbol (U+00B5)/greek mu (U+03BC), and the true minus character (U+2212)?

ER