Code covered by the BSD License  

Highlights from
Convert Between Numerics and Prefixed Strings (SI or Binary)

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 7.66 KB File ID: #33174
image thumbnail

Convert Between Numerics and Prefixed Strings (SI or Binary)

by Stephen Cobeldick

 

07 Oct 2011 (Updated 12 Feb 2013)

Convert between numeric scalar values and SI prefixed or binary prefixed strings.

| Watch this File

File Information
Description

### Functions ###

Four functions provide conversions between scalar numeric values and strings with coefficients and prefixes, e.g.: 1000 -> '1 k' and '1 k' -> 1000.

The functions are:
# binum: Binary prefixed string => scalar numeric.
# bipre: Scalar numeric => Binary prefixed string.
# sinum: SI (metric) prefixed string => scalar numeric.
# sipre: Scalar numeric => SI (metric) prefixed string.

Numeric to String:
# Prefix may be selected to be either the full name or the symbol (symbol is default).
# Coefficient's significant figures may be selected (four is default).
# Function automatically uses the most suitable prefix.

String to Numeric:
# Prefix may be automatically detected as name or symbol, or user selected.
# Coefficient format allows +/-, decimal and e-notation.
# Also returns any units that follow the prefix.

### Examples ###

sipre(1000)
 ans = '1 k'

sinum('1 k')
 ans = 1000

bipre(1024)
 ans = '1 Ki'

binum('1 Ki')
 ans = 1024

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

[val,uni] = sinum('Power: 200 megawatt')
 val = 200000000
 uni = 'watt'

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

[val,uni] = binum('Memory: 200 mebibyte')
 val = 209715200
 uni = 'byte'

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

Compared to similar functions available on File Exchange, these functions have the correct:
(Num->Str)
# Space character between the coefficient and the prefix.
# Space character after the coefficient, even if no prefix (try 1).
# Significant figure rounding (try 0.99 or 999E3, with 1 significant figure).
# No prefix for zero and values outside the prefix range (try 0, Inf, 1E30).
(Str->Num)
# Symbol upper/lowercase.
# Conversion of negative strings (try '-1').
# Conversion of E-notation values (try '1E0', '1E0 k', '1E30').

Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
Tags for This File  
Everyone's Tags
atto, binary, bytes, communications, data, engineering, exa, exbi, femto, gibi, giga, iso 80000, kibi, kilo, mega, metric, mibi, micro, milli, nano, notation, pebi, peta, pico, prefix, si, tebi, tera, yobi, yocto, yotta, zebi, zepto, zetta
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
09 Nov 2011

Edit file description to be more accurate.

14 Feb 2012

Provide useage examples.

24 Aug 2012

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

27 Aug 2012

- Fix edge case (val=1).

19 Nov 2012

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

03 Dec 2012

- 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

- Edit the help section a little.

Contact us