Documentation Center

  • Trial Software
  • Product Updates

format

Set display format for output

Syntax

Description

example

format sets the display of floating-point numeric values to the default display format, which is the short fixed decimal format. This format displays 5-digit scaled, fixed-point values.

The format function affects only how numbers display in the Command Window, not how MATLAB® computes or saves them.

example

format Style changes the display format to the specified Style.

Examples

expand all

Change and View Current Format

Set the display format to long fixed decimal.

format long

View the result for the value of pi.

pi
ans =

   3.141592653589793

View the current format.

get(0,'format')
ans =

long

Display Values in Default and Hexadecimal Format

Set the format to its default, and display the maximum values for integers and real numbers in MATLAB.

format

intmax('uint64')
ans =

 18446744073709551615
realmax
ans =
  1.7977e+308

Change the display format to hexadecimal, and then display the same values.

format hex

intmax('uint64')
ans =

   ffffffffffffffff
realmax
ans =

   7fefffffffffffff

The hexadecimal display corresponds to the internal representation of the value. It is not the same as the hexadecimal notation in the C programming language.

View Output in Short and Long Engineering Notation

View the difference between output displayed in the shortEng and longEng formats.

Set the display format to shortEng.

format shortEng

Create variable A and increase its value by a multiple of 10 each time through a for loop. Display each value of A.

A = 5.123456789;
for k=1:10
   disp(A)
   A = A * 10;
end
     5.1235e+000
    51.2346e+000
   512.3457e+000
     5.1235e+003
    51.2346e+003
   512.3457e+003
     5.1235e+006
    51.2346e+006
   512.3457e+006
     5.1235e+009

The values for A display with 4 digits after the decimal point, and an exponent that is a multiple of 3.

Set the display format to longEng and view the same values of A.

format longEng

A = 5.123456789;
for k=1:10
   disp(A)
   A = A * 10;
end
    5.12345678900000e+000
    51.2345678900000e+000
    512.345678900000e+000
    5.12345678900000e+003
    51.2345678900000e+003
    512.345678900000e+003
    5.12345678900000e+006
    51.2345678900000e+006
    512.345678900000e+006
    5.12345678900000e+009

The values for A display with 15 digits, and an exponent that is a multiple of 3.

Display Large Data Range in short and shortg Formats

View the difference between the short and shortg formats when the values in a matrix span a wide range.

Define variable x and display the output in the short format.

x = [25 56 255 9876899999];
format short
x
x =
   1.0e+09 *
    0.0000    0.0000    0.0000    9.8769

The display indicates each value is multiplied by 1.0e+09.

Set the format to shortg and redisplay x.

format shortg
x
x =
           25           56          255   9.8769e+09

Input Arguments

expand all

Style — Output display formatshort (default) | long | shortE | longE | ...

Output display format, specified as one of the strings listed in the tables that follow.

Use these styles to switch between different output display formats for floating-point variables. Styles are case insensitive. You also can insert a space between short or long and the presentation type, for instance, format short E.

Style

Result

Example

short (default)

Short fixed decimal format, with 4 digits after the decimal point.

If you are displaying a matrix with a wide range of values, consider using shortG. See Display Large Data Range in short and shortg Formats

3.1416

long

Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

3.141592653589793

shortE

Short scientific notation, with 4 digits after the decimal point.

Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.

3.1416e+00

longE

Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.

3.141592653589793e+00

shortG

The more compact of short fixed decimal or scientific notation, with 5 digits.

3.1416

longG

The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.

3.14159265358979

shortEng

Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.

3.1416e+000

longEng

Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.

3.14159265358979e+000

Use these format styles to switch between different output display formats for all numeric variables.

Style

Result

Example

+

Positive/Negative format, with +, -, and blank characters displayed for positive, negative, and zero elements.

+

bank

Currency format, with 2 digits after the decimal point.

3.14

hex

Hexadecimal representation of a binary double-precision number.

400921fb54442d18

rat

Ratio of small integers.

355/113

Use these format styles to affect the spacing in the display of all variables.

DispType

Result

Example

compact

Suppresses excess line feeds to show more output in a single screen. Contrast with loose.

theta = pi/2
theta =
  1.5708

loose

Adds linefeeds to make output more readable. Contrast with compact.

theta = pi/2

theta =

  1.5708

More About

expand all

Tips

  • The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose a Numeric format or Numeric display option in the Command Window Preferences.

  • To see which Style is currently in use, type

    get(0,'Format')

    To see if compact or loose formatting is currently selected, type

    get(0,'FormatSpacing')

Algorithms

MATLAB always displays integer variables to the appropriate number of digits for the class. For example, MATLAB uses 3 digits to display numbers of type int8 (for example, -128:127). Setting format to short or long does not affect the display of integer variables.

If the largest element of a matrix is larger than 103 or smaller than 10-3, then MATLAB applies a common scale factor for the short and long formats.

See Also

| | | | | | | | | |

Was this topic helpful?