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 painfully inefficient. NUM2SIP has been tested against a large set of test cases, including many edge-cases and with all of the optional arguments.

In the NUM2SIP and NUM2BIP documentation the word string refers to a 1xN character vector, and not to the MATLAB string class.

Contents

Basic Usage

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

num2sip(1000)
num2sip(1.2e+3)
num2sip(456e-7)
ans =
1 k
ans =
1.2 k
ans =
45.6 µ

2nd Input: Significant Figures

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

num2sip(987000,3)
num2sip(987000,2)
num2sip(987000,1)
ans =
987 k
ans =
990 k
ans =
1 M

3rd Input: Symbol or Full Prefix

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

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

3rd Input: Fixed Prefix

By default NUM2SIP selects the most appropriate prefix. The optional third input argument lets the use specify the prefix. For convenience the "micro" symbol may be provided as 'u' or (U+00B5) or (U+03BC).

num2sip(1e2,[],'k')
num2sip(1e4,[],'k')
num2sip(1e6,[],'k')
ans =
0.1 k
ans =
10 k
ans =
1000 k

4th Input: Trailing Zeros

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

num2sip(1e3,3,[],false) % default
num2sip(1e3,3,[],true)
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:

num2sip(9e-87)
num2sip(2e+34)
ans =
9e-87 
ans =
2e+34 

Micro Symbol

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

num2sip(5e-6) % default = (U+00B5) 'MICRO SIGN'
ans =
5 µ

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 just the unit, i.e. no SI prefix. NUM2SIP correctly includes the space character in all cases (by default using (U+00A0) 'NO-BREAK SPACE'):

sprintf('%sV',num2sip(1e-3))
sprintf('%sV',num2sip(1e+0))
sprintf('%sV',num2sip(1e+3))
sprintf('%sV',num2sip(1e99))
ans =
1 mV
ans =
1 V
ans =
1 kV
ans =
1e+99 V

Bonus: NUM2BIP Binary Prefix Function

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

The function NUM2BIP has exactly the same arguments as NUM2SIP:

num2bip(1024)
num2bip(1025,5,true,true)
ans =
1 Ki
ans =
1.0010 kibi

Bonus: NUM2RKM RKM-Code Function

The submission includes the bonus function NUM2RKM: this converts a numeric scalar to a prefixed string using IEC 60062 specified RKM code instead of metric prefixes. RKM code is used for resistors and capacitors.

num2rkm(4700)
num2rkm(15e6,3)
ans =
4k7
ans =
15M0

Reverse Conversion: String to Numeric

The functions SIP2NUM and BIP2NUM and RKM2NUM convert from string into numeric:

bip2num('1.25 Ki')
sip2num('1.25 k')
rkm2num('1k25')
ans =
        1280
ans =
        1250
ans =
        1250