Code covered by the BSD License

### Highlights from Prefixed-String Conversion (SI or Binary)

Be the first to rate this file! 19 Downloads (last 30 days) File Size: 8.58 KB File ID: #33174 Version: 1.13

# Prefixed-String Conversion (SI or Binary)

### Stephen Cobeldick (view profile)

07 Oct 2011 (Updated )

Convert between numeric values and SI (or binary) prefixed strings.

File Information
Description

Four convenient functions convert between numeric values and strings with coefficients and prefixes, e.g.: 1000 -> '1 k' and '1 k' -> 1000.
Particularly useful for helping create publications following the SI standard, particularly where control over significant digits and trailing zeros is required.

### Features and Options ###

Numeric to String Conversion:
- Automatically selects the most suitable prefix.
- Rounds to the requested significant figures.
- Prefix may be selected as either the full name or the symbol.
- Trailing decimal zeros of the coefficient may be included or removed.

String to Numeric Conversion:
- Prefix may be defined as either the name or symbol, or automatically detected.
- Coefficient may include +/- 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.

### SI Prefix Examples ###

sipre(10000)
ans = '10 k'

sinum('10 k')
ans = 10000

sipre(12345,6,true,true)
ans = '12.3450 kilo'

sinum('12.3450 kilo')
ans = 12345

### Binary Prefix Examples ###

bipre(10240)
ans = '10 Ki'

binum('10 Ki')
ans = 10240

bipre(12345,6,true,true)
ans = '12.0557 kibi'

binum('12.0557 kibi')
ans = 12345

### SI Prefixes (Metric/Engineering) ###

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 submssions 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).
- parse negative strings (try '-1').
- parse any combination of E-notation values (try '1e0', '1e0 k', '1e30').

Acknowledgements
Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
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.