Navigation

Operators and Keywords

Function List:

C++ API

Function File: num2str (x)
Function File: num2str (x, precision)
Function File: 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"

Notes:

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

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

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

See also: sprintf, int2str, mat2str.

Package: octave