# Scientific Prefix to Number

Versione 4.1.5 (21,5 KB) da
Convert SI-prefixed text (aka engineering / metric prefix) into numeric values. Bonus: binary prefixes!
Aggiornato 12 feb 2024

Visualizza la licenza

The function SIP2NUM converts a string with an SI prefix (aka metric prefix, or engineering prefix) into a numeric value. 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 many 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 restricted to either a 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 and prefixes.
• Returns the number of significant figures detected in the coefficients.
• Includes the prefixes added in November 2022: ronna, quetta, ronto, and quecto.
Reverse Conversion
SI Prefix Examples
>> sip2num('10 k') % OR sip2num('10.0 kilo') OR sip2num('10000') OR sip2num('1e4')
ans = 10000
>> [num,spl] = sip2num("Power: 200 megawatt")
num = 200000000
spl = ["Power: ","watt"]
>> [num,spl,sgf] = sip2num("from -3.6 MV to +1.24kV potential difference.")
num = [-3600000,1240]
spl = ["from ","V to ","V potential difference."]
sgf = [2,3]
>> [num,spl] = sip2num("100 meter","meter") % Try it without the second option.
num = 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
>> [num,spl] = bip2num("Memory: 200 mebibyte")
num = 209715200
spl = ["Memory: ","byte"]
>> [num,spl,sgf] = bip2num("From -3.6 MiB to +1.24KiB data allowance.")
num = [-3774873.6,1269.76]
spl = ["From ","B to ","B data allowance."]
sgf = [2,3]
>> [num,spl] = bip2num("100 Pixel","Pixel") % Try it without the second option.
num = 100
spl = ["","Pixel"]
>> bip2num(num2bip(pow2(9,40))) % 9 tebi == pow2(9,40) == 9*1024^4
ans = 9895604649984
SI Prefixes (Bureau International des Poids et Mesures)
Magnitude | Symbol | Name
1000^-10 | q | quecto
1000^-9 | r | ronto
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
1000^+9 | R | ronna
1000^+10 | Q | quetta
Binary Prefixes (IEC 60027-2 A.2 and ISO/IEC 80000-13:2008)
Magnitude | Symbol | Name
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 E-notation values (try '1e0', '1e0 k', '1e30').

### Cita come

Stephen23 (2024). Scientific Prefix to Number (https://www.mathworks.com/matlabcentral/fileexchange/53886-scientific-prefix-to-number), MATLAB Central File Exchange. Recuperato .

##### Compatibilità della release di MATLAB
Creato con R2010b
Compatibile con R2009b e release successive
##### Compatibilità della piattaforma
Windows macOS Linux
##### Categorie
Scopri di più su Data Type Conversion in Help Center e MATLAB Answers

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Versione Pubblicato Note della release
4.1.5

* update image.

4.1.4

* Documentation improvements.
* Example RKM function.

4.1.3

* Improve test function robustness.

4.1.2

4.1.1

* Update screenshot.

4.1.0

* Optimisations for code performance.
* Improve test function robustness.

4.0.2

* Add plenty of new testcases.

4.0.1

* Fix HTML table formatting.

4.0.0

* Accept string type inputs.
* Includes prefixes ronna, quetta, ronto and quecto.
* Improve error messages and IDs.

3.2.1

3.2.0

* Case-insensitive matching of INF and NaN (just like SSCANF etc).

3.1.3

* More test cases.
* More robust whitespace handling.

3.1.2

* Update documentation.
* Improve significant figure measurement.

3.1.1

* Improve documentation.
* Improve significant figure counting.

3.1.0

* Parse micro characters U+00B5 & U+03BC.
* Parse negative character U+2212.

3.0.1

* Improve documentation.

3.0.0.0

* Simplify algorithm: faster!

2.1.0.0

* Change second input true/false to match NUM2SIP and NUM2BIP.