The function NUM2SIP converts a numeric scalar into a string with a metric prefix (aka SI prefix, or engineering prefix). For example the value 1000 is converted to '1 k'.
After testing the metric-prefix submissions on MATLAB FEX (see Acknowledgements) and not finding a single one that converted all values correctly and that supported the correct SI spacing, I wrote my own functions. And then exhaustively tested them to confirm that they actually give the correct output.
* Always includes the space character (required by the SI standard).
* Automatically selects the most suitable prefix.
* Rounds to the requested significant figures (default==5).
* Prefix may be selected as either the full name ('kilo') or the symbol ('k').
* Option to include or remove trailing decimal zeros.
* Rounds up to the next prefix when significant figures require, e.g. '1 M', not '1000 k'.
### Bonus Functions ###
The function NUM2BIP converts from numeric to binary-prefixed string, for example the value 1024 is converted to '1 Ki'.
The function NUM2RKM converts from numeric to RKM code, for example the value 1200 is converted to '1k2'.
### Reverse Conversion ###
To convert from metric-prefixed string to numeric:
### Metric Prefix Examples ###
>> num2sip(10000) OR num2sip(1e4)
ans = '10 k'
ans = '10 kilo'
ans = '10.00 k'
ans = '999 '
ans = '1 k'
ans = '500 k'
ans = '0.5 M'
>> ['Power: ',num2sip(200e6,,true),'watt']
ans = 'Power: 200 megawatt'
>> sprintf('Clock frequency is %shertz.',num2sip(1234567890,3,true))
ans = 'Clock frequency is 1.23 gigahertz.'
>> num2sip(sip2num('9 T')) % 9 tera == 9e12 == 9*1000^4
ans = '9 T'
### Binary Prefix Examples ###
>> num2bip(10240) OR num2bip(1.024e4) OR num2bip(pow2(10,10)) OR num2bip(10*2^10)
ans = '10 Ki'
ans = '10 kibi'
ans = '10.00 Ki'
ans = '1020 '
ans = '1 Ki'
ans = '512 Ki'
ans = '0.5 Mi'
>> ['Memory: ',num2bip(pow2(200,20),,true),'byte']
ans = 'Memory: 200 mebibyte'
>> sprintf('Data saved in %sbytes.',num2bip(1234567890,3,true))
ans = 'Data saved in 1.15 gibibytes.'
>> num2bip(bip2num('9 Ti')) % 9 tebi == pow2(9,40) == 9*1024^4
ans = '9 Ti'
### 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^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^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 rounding for all choices of significant figures. Compared to similar submissions available on MATLAB File Exchange, these functions correctly:
* include the space character between the coefficient and the prefix, even if there is no prefix (try 1).
* round to the requested significant figures (try 0.999 or 999e3, with 1 or 2 significant figures).
* return a coefficient without a prefix for zero and values outside the prefix range (try 0, Inf, 1E30).
* return a coefficient without an exponent when the significant digits are less than three (try 1e5 to 1 sigfig).
Stephen Cobeldick (2021). Number to Scientific Prefix (https://www.mathworks.com/matlabcentral/fileexchange/33174-number-to-scientific-prefix), MATLAB Central File Exchange. Retrieved .
@Collin Pecora: a little bit late, but I changed the default "micro" symbol to 'µ', i.e. char(181). The Mfile can be edited to use 'u' if required.
nevermind.... you already have the inverse functionality integrated in another function. ta
Hi Stephen, I'd love to see an option or another function where you do the exact inverse. --> 1ms --> 0.001s. Basically make a 'remove scientific prefix'
Hi @Stephen Cobeldick - I've been trying to get hold of you but I've not been able to find any contact info anywhere. I recently submitted my own num2eng function; if I'd found your num2sip before I'd pretty much finished my version I'm sure I wouldn't have bothered writing it! As it is though, I was already using my own function in a number of other projects so it made sense to stick with mine. Your submission did help me make some final improvements though. My email can be found in the comment box of my submitted file. Please drop me a line if you feel so inclined.
@Collin Pecora. Thank you for your feedback. The function returns 'u' because I wanted to stick to ASCII characters only, as different file formats and character encoding are not always compatible. You can easily edit the file or use |strrep| to get the result that you want.
Could chage micro 'u' to char(181) in your prefix cell array
Inspired by: num2eng, eng2num, num2eng, num2str with metric prefix, num2sci, sci2num, Customizable Natural-Order Sort, MetricPrefixStr - Print SI Prefix Formatted Numbers, Number to Words, Words to Number
Inspired: Words to Number, Round to Electronic Component Values, num2eng, Vibration Nomogram, NUM2ENG: Fast number to engineering notation (SI) conversion, Number to Myriad, Customizable Natural-Order Sort, Numeric to Ordinal-String, Number to Words, Natural-Order Row Sort, Natural-Order Filename Sort, Interactive Regular Expression Tool
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!