Number to Scientific Prefix

Version 5.0.0 (29.6 KB) by Stephen23
Convert a numeric value to SI-prefixed text (aka engineering / metric prefix). Bonus: binary prefixes!
1.7K Downloads
Updated 17 Dec 2024

View License

The function NUM2SIP converts a numeric scalar into a string with an SI prefix (aka metric prefix, or engineering prefix). For example the value 1000 is converted to '1 k'. The bonus function NUM2BIP converts from numeric to binary-prefixed string, for example the value 1024 is converted to '1 Ki'.
After testing many 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.
These functions are particularly useful to help create publications following SI standards.
This submission:
  • 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').
  • Trailing decimal zeros of the coefficient may be included or removed.
  • Rounds up to the next prefix when significant figures require, e.g. '1 M', not '1000 k'.
  • Also returns the numeric coefficient and the prefix separately.
  • Includes the prefixes added in November 2022: ronna, quetta, ronto, and quecto.
Reverse Conversion
SI Prefix Examples
>> num2sip(10000) % OR num2sip(1e4) OR num2sip(power(10,4)) OR num2sip(10^4)
ans = '10 k'
>> num2sip(10000,'name')
ans = '10 kilo'
>> num2sip(10000,4,'trz','string')
ans = "10.00 k"
>> num2sip(999,4)
ans = '999 '
>> num2sip(999,3)
ans = '999 '
>> num2sip(999,2)
ans = '1 k'
>> num2sip(999,1)
ans = '1 k'
>> num2sip(0.5e6)
ans = '500 k'
>> num2sip(0.5e6,'M')
ans = '0.5 M'
>> sprintf('Power: %swatt', num2sip(200e6,'name'))
ans = 'Power: 200 megawatt'
>> sprintf('Clock frequency is %sHz.', num2sip(1234567890,3))
ans = 'Clock frequency is 1.23 GHz.'
>> 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'
>> num2bip(10240,'name')
ans = '10 kibi'
>> num2bip(10240,4,'trz','string')
ans = "10.00 Ki"
>> num2bip(1023,4)
ans = '1023 '
>> num2bip(1023,3)
ans = '1020 '
>> num2bip(1023,2)
ans = '1 Ki'
>> num2bip(1023,1)
ans = '1 Ki'
>> num2bip(pow2(19))
ans = '512 Ki'
>> num2bip(pow2(19),'Mi')
ans = '0.5 Mi'
>> sprintf('Memory: %sbyte', num2bip(pow2(200,20),'name'))
ans = 'Memory: 200 mebibyte'
>> sprintf('Data saved in %sB.', num2bip(1234567890,3))
ans = 'Data saved in 1.15 GiB.'
>> num2bip(bip2num('9 Ti')) % 9 tebi == pow2(9,40) == 9*1024^4
ans = '9 Ti'
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 rounding to the requested significant figures. Compared to similar submissions available on MATLAB File Exchange, these functions correctly:
  • include the space character following the coefficient, even if there is no prefix (try value 1).
  • round to the requested significant figures (try 0.999 or 999e3, with 1 or 2 sigfig).
  • return a coefficient without a prefix for zero and values outside the prefix range (try values 0, 7, Inf, 1E99).
  • return a coefficient without exponent notation when the significant digits are less than three (try 1e5 to 1 sigfig).

Cite As

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

MATLAB Release Compatibility
Created with R2010b
Compatible with R2009b and later releases
Platform Compatibility
Windows macOS Linux
Categories
Find more on Data Type Conversion in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
5.0.0

* New options and option parsing.
* Revised outputs.

4.1.5

* update image.

4.1.4

* Documentation improvements.
* Example RKM function.

4.1.3

* Improve test function robustness.

4.1.2

* Correct SEE ALSO references.

4.1.1

* Update screenshot.

4.1.0

* Add 3rd and 4th output arguments.
* Optimisations for code performance.
* Improve test function robustness.
* Add more test cases.

4.0.2

* Fix bug with fixed-prefix special-case values (0, Inf, NaN).
* 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

* Add error IDs.

3.2.0

* Add second output argument that indicates if a prefix is used or not.
* Add bonus function NUM2RKM.

3.1.3

* More test cases.
* More robust whitespace handling.

3.1.2

* Update documentation.
* Default non-breaking space.

3.1.1

* Improve documentation.

3.1.0

* Return micro character 'µ' (rather than 'u').

3.0.1

* Improve documentation.

3.0.0.0

* Add "fixed" prefix input option.
* Rewrite algorithm to use local functions.

2.1.0.0

* Add HTML documentation.
* Empty inputs use default values.

2.0.0.0

* Split subimssion into two: one for num->str, other for str->num.
* Rename all functions.

1.13.0.0

- Improve handling of trailing zeros.
- Improve input checking.

1.12.0.0

- Default is now lower-case E-notation.

1.11.0.0

- Add control over allowing trailing zeros in string.
- Convert all string coefficients into numeric (not just the first one).
- Return split string parts as a cell array.

1.10.0.0

- Edit the help section a little.

1.9.0.0

- Str->Num: space character between coefficient and prefix is optional.
- Str->Num: NaN and Inf may occur with prefixes.
- Help: Simplify prefix tables.

1.8.0.0

Upload all four functions for converting between numeric and string, for both SI and binary.

1.6.0.0

- Fix edge case (val=1).

1.5.0.0

- Allow positive/negative input value.
- Consistent handling of Inf/NaN values.

1.3.0.0

Provide useage examples.

1.1.0.0

Edit file description to be more accurate.

1.0.0.0