View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Number to Scientific Prefix

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0
5.0 | 2 ratings Rate this file 16 Downloads (last 30 days) File Size: 9.12 KB File ID: #33174 Version: 2.1
image thumbnail

Number to Scientific Prefix

by

 

07 Oct 2011 (Updated )

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

| Watch this File

File Information
Description

The fucntion 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'. The bonus function NUM2BIP converts from numeric to binary-prefixed string, for example the value 1024 is converted to '1 Ki'.

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.

These functions are particularly useful to help create publications following SI standards, or where control over significant digits and trailing zeros is required, or for including numeric values in figures.

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'.
* Uses simple integer even if precision is less than three (no exponent).

To convert from metric-prefixed string to numeric:
http://www.mathworks.com/matlabcentral/fileexchange/53886-scientific-prefix-to-number

### Metric Prefix Examples ###

num2sip(10000)
 ans = '10 k'
num2sip(10000,4,true)
 ans = '10 kilo'
num2sip(10000,4,false,true)
 ans = '10.00 k'

num2sip(999,2)
 ans = '1 k'
num2sip(999,3)
 ans = '999 '

num2sip(-5.555e9,3)
 ans = '-5.56 G'

['Power: ',num2sip(200*1000^2,[],true),'watt']
 ans = 'Power: 200 megawatt'

sprintf('Clock frequency is %shertz.',num2sip(1234567890,8,true))
 ans = 'Clock frequency is 1.2345679 gigahertz.'
 
### Binary Prefix Examples ###

num2bip(10240)
 ans = '10 Ki'
num2bip(10240,4,true)
 ans = '10 kibi'
num2bip(10240,4,false,true)
 ans = '10.00 Ki'

num2bip(1023,3)
 ans = '1020 '
num2bip(1023,2)
 ans = '1 Ki'

num2bip(-5.555e9,3)
 ans = '-5.17 Gi'

['Memory: ',num2bip(200*1024^2,[],true),'byte']
 ans = 'Memory: 200 mebibyte'

sprintf('Data saved in %sbytes.',num2bip(1234567890,8,true))
 ans = 'Data saved in 1.1497809 gibibytes.'

### 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 | u / 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 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).

Acknowledgements

Num2eng, Eng2num, Num2eng, Num2str With Metric Prefix, Num2sci, Sci2num, Metric Prefix Str Print Si Prefix Formatted Numbers, Customizable Natural Order Sort, Number To Words, Natural Order Row Sort, Natural Order Filename Sort, and Words To Number inspired this file.

This file inspired Iso 8601 Date String To Serial Date Number, Date Vector/Number To Iso 8601 Date String, Number To Myriad, Natural Order Row Sort, Natural Order Filename Sort, Customizable Natural Order Sort, Interactive Regular Expression Maker, Numeric To Ordinal String, Number To Words, and Words To Number.

Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
MATLAB Search Path
/
/html
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
19 May 2016 Stephen Cobeldick

@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.

Comment only
15 May 2016 Collin Pecora

Very helpfull.
Could chage micro 'u' to char(181) in your prefix cell array

15 May 2016 Collin Pecora  
Updates
09 Nov 2011 1.1

Edit file description to be more accurate.

14 Feb 2012 1.3

Provide useage examples.

24 Aug 2012 1.5

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

27 Aug 2012 1.6

- Fix edge case (val=1).

19 Nov 2012 1.8

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

03 Dec 2012 1.9

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

12 Feb 2013 1.10

- Edit the help section a little.

06 Jan 2014 1.11

- 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.

10 Feb 2014 1.12

- Default is now lower-case E-notation.

14 Jul 2014 1.13

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

09 Nov 2015 2.0

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

28 Aug 2016 2.1

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

Contact us