MATLAB Numeric Display
When MATLAB prints a result in the Command Window, the digits you see are a rounded picture of a number, not the number itself. A variable of class double holds roughly 15 to 17 significant decimal digits, yet the default display shows only four digits after the decimal point. The format command chooses how many of those digits appear and in what style. If you know exactly what it changes and what it leaves alone, you avoid a classic error: copying rounded screen values back into a calculation.
Stored value versus displayed value
A display format is the rule MATLAB applies when it turns a stored number into text on the screen. Running a format command changes only how values appear, not the values themselves. Every later calculation still uses the full stored double.
For the fixed styles, d counts digits after the decimal point. For the G styles described below, it counts significant digits.
Example: With the default format, x = pi displays as 3.1416. Typing x - 3.1416 does not give zero. It gives -7.3464e-06, because x still holds 3.141592653589793 and only the screen was rounded.
The main format styles
The table shows how each style displays the same stored value, pi.
Command | Rule | Display of pi |
|---|---|---|
format short (default) | fixed, 4 digits after the point | 3.1416 |
format long | fixed, 15 digits after the point | 3.141592653589793 |
format shortE | scientific, 4 digits after the point | 3.1416e+00 |
format longE | scientific, 15 digits after the point | 3.141592653589793e+00 |
format shortG | 5 significant digits, fixed or scientific | 3.1416 |
format longG | 15 significant digits, fixed or scientific | 3.14159265358979 |
format bank | fixed, 2 digits after the point | 3.14 |
format rat | ratio of small integers | 355/113 |
The E styles always write a mantissa followed by a power of ten, so e+00 means ×100. The G styles pick fixed or scientific notation, whichever is more compact, and count significant digits rather than decimal places. That is why format longG shows one digit fewer of π than format long: its 15 significant digits include the leading 3.
format bank suits currency amounts. format rat is also an approximation: 355/113 is close to π but not equal to it, and the variable is still stored as a double.
Two further behaviours are worth recognising. Whole-number doubles print without a decimal point, so n = 50 shows 50 under both short and long. Very large or very small magnitudes switch to scientific notation even under format short, and a matrix with widely varying entries may print a common scale factor such as 1.0e+03 * above its columns.
Choosing and resetting a format
- Use format long or format longG to check whether two results really agree. Under format short, two values that differ by almost 0.0001 can display identically.
- format compact removes the blank lines MATLAB puts around output, and format loose restores them. These control spacing only, never digits.
- format with no argument restores the default, short and loose.
- A format stays in effect for the rest of the session until you change it.
- Text produced by fprintf or sprintf follows its own format specifier, so the format setting does not affect it.
What to remember
- format changes the screen text only; stored doubles and calculations are unaffected.
- format short and format long fix the digits after the decimal point (4 and 15); format shortG and format longG fix significant digits (5 and 15).
- The E styles always use scientific notation, format bank shows two decimals, and format rat shows a fraction that approximates the value.
- Reuse variables instead of retyping displayed digits.
- format alone resets the default display.
Example: Flow rate on screen versus in memory
A lab script records that 50 L of water passes a flow meter in 7 s and computes the average flow rate with q = 50/7, in litres per second. To 16 significant digits the stored double is 7.142857142857143.
a) What does MATLAB display for q under format short and under format bank?
format short keeps four digits after the decimal point. The digits after the point are 1, 4, 2, 8 and then 5, 7 and so on, so the fifth digit is ________ and the fourth digit rounds up from 8 to 9:
Under format short the display is q = 7.1429.
format bank keeps two digits after the point. The third digit is ________, so the value rounds down and the display is q = 7.14.
b) You then run format long and type V = q*7. What does MATLAB display for q and for V, and what does this tell you about part a)?
format long shows ________ digits after the point, so q now displays as q = 7.142857142857143. These are the same digits that were stored all along; the short and bank displays in part a) never rounded the variable.
The product uses that full stored value. The exact product of the stored double and 7 lies within rounding distance of 50, and double-precision arithmetic returns exactly 50. A whole-number double prints without a decimal point, so MATLAB displays V = 50.
Changing the format revealed digits that were already present, and the arithmetic was never limited to the four digits shown earlier.
c) A labmate reads 7.1429 off the screen and types V2 = 7.1429*7 under format short. What does MATLAB display, and how large is the volume error compared with V?
The retyped number is larger than the stored flow rate by about ________ L/s:
Multiplying the retyped value by 7 gives 50.0003, and because that is not a whole number, format short shows it with four decimals:
The display is V2 = 50.0003. The volume error is
which is ________ times the rounding error in the retyped flow rate. The error came from copying screen digits, not from MATLAB; typing q*7 avoids it entirely.
A strain gauge reading is stored in MATLAB as s = 0.000452189. You switch the Command Window to the short and long scientific-notation formats and display s under each one.
Under format short e, the Command Window shows the value of s as , and under format long e the mantissa shows digits after its decimal point.
In MATLAB, a student types r = 2/3 and then displays r after each format command listed in the table. To 17 significant digits the stored double is 0.66666666666666663. Enter exactly the number MATLAB shows for r under each command, without the r = part.
| Command | Displayed value |
| format short | |
| format shortE | |
| format bank | |
| format shortG | |
| format rat |