NUM2SIP and NUM2BIP Examples

The function NUM2SIP converts a numeric scalar to a character vector of the number value with a metric prefix, for example 1000 -> '1 k'. Optional arguments control the number of digits, select the prefix symbol or prefix name, and any trailing zeros: this document shows examples of how to use these features.

The development of NUM2SIP was motivated by the lack of any well- written function that provides this conversion: many of the functions available on FEX do not conform to the SI standard, or use buggy conversion algorithms, or are simply painfully slow. NUM2SIP has been tested against a large set of test cases, including many edge-cases and with all of the optional arguments.


Basic Usage

In many cases NUM2SIP can be called with just a numeric value:

ans =
1 k
ans =
1.2 k
ans =
45.6 µ

2nd Input: Significant Figures

NUM2SIP returns five significant figures by default. The optional second input argument specifies the number of significant figures. Note that NUM2SIP correctly rounds upwards to the next prefix:

ans =
987 k
ans =
990 k
ans =
1 M

3rd Input: Symbol or Full Prefix

NUM2SIP returns the prefix symbol by default. The optional third input argument selects between the symbol and the full prefix name.

num2sip(1e6,[],false) % default
ans =
1 M
ans =
1 mega

3rd Input: Fixed Prefix

NUM2SIP allow the prefix to be selected by the user, and all outputs will be given as coefficients of the selected prefix. The "micro" symbol may be provided as 'u' or (U+00B5) or (U+03BC).

ans =
0.1 k
ans =
10 k
ans =
1000 k

4th Input: Trailing Zeros

NUM2SIP removes trailing zeros by default. The optional fourth input argument selects between removing and keeping any trailing zeros:

num2sip(1000,3,[],false) % default
ans =
1 k
ans =
1.00 k

Larger/Smaller Values Without a Prefix

If the magnitude of the input value is outside the prefix range, then no prefix is used and the value is returned in exponential notation:

ans =
ans =

Space Character

The standard for the International System of Quantities (ISQ) ISO/IEC 80000 (previously ISO 31) specifies that "there is a space between the numerical value and the unit symbol". Note that this applies even when there is no SI prefix, just the unit. NUM2SIP correctly includes the space character in all cases:

ans =
1 mV
ans =
1 V
ans =
1 kV
ans =
1e+99 V

Micro Symbol

By default NUM2SIP uses the "micro" symbol from ISO 8859-1, i.e. Unicode (U+00B5) 'MICRO SIGN'. The user can edit the Mfile to select alternative "micro" symbols: either ASCII 'u' or (U+03BC) 'GREEK SMALL LETTER MU'.

ans =
1 µ

NUM2BIP (Binary Prefix)

The submission includes the bonus function NUM2BIP: this also converts a numeric scalar to a prefixed string, but uses the ISO 80000 defined binary prefixes instead of metric prefixes. Binary prefixes are mostly used for computer memory.

The function NUM2BIP has exactly the same arguments as NUM2SIP:

ans =
1 Ki
ans =
1.0010 kibi

Reverse Conversion: String to Numeric

The functions SIP2NUM and BIP2NUM convert from prefixed strings into numerics:

sip2num('10 M')
bip2num('10 Mi')
ans =
ans =