Navigation

Operators and Keywords

Function List:

C++ API

: num2str (x)
: num2str (x, precision)
: num2str (x, format)

Convert a number (or array) to a string (or a character array).

The optional second argument may either give the number of significant digits (precision) to be used in the output or a format template string (format) as in sprintf (see ‘Formatted Output’). num2str can also process complex numbers.

Examples:

num2str (123.456)
     ⇒ "123.46"

num2str (123.456, 4)
     ⇒ "123.5"

s = num2str ([1, 1.34; 3, 3.56], "%5.1f")
     ⇒ s =
        1.0  1.3
        3.0  3.6
whos s
     ⇒
      Attr Name        Size                     Bytes  Class
      ==== ====        ====                     =====  =====
           s           2x8                         16  char

num2str (1.234 + 27.3i)
     ⇒ "1.234+27.3i"

The num2str function is not very flexible. For better control over the results, use sprintf (see ‘Formatted Output’).

Programming Notes:

For MATLAB compatibility, leading spaces are stripped before returning the string.

Integers larger than flintmax may not be displayed correctly.

For complex x, the format string may only contain one output conversion specification and nothing else. Otherwise, results will be unpredictable.

Any optional format specified by the programmer is used without modification. This is in contrast to MATLAB which tampers with the format based on internal heuristics.

See also: sprintf, int2str, mat2str.

Package: octave