# Displaying Numbers in MATLAB7

Posted by Loren Shure,

The way MATLAB displays numbers sometimes confuses users. Occasionally someone posts a concern to the MATLAB newsgroup that the calculation just performed was done to only 4 digits but the user expected more decimals. Assuming the code is "typical" in some vague sense, the user's numeric information at this point is almost always sitting in double precision MATLAB variables.

### Numeric Formats in MATLAB

Helpful posts typically follow up with the explanation that the display of output is controlled by the format setting and that the default numeric variables in MATLAB are indeed double precision, holding about 15 significant decimal digits. By default, MATLAB displays floating point values using what's called a scaled fixed point format, resulting in 4 digits displayed after the decimal point.

Here's the value of pi displayed with the default format in MATLAB.

format
mypi = pi
mypi =

3.1416



and we can see that the variable mypi is stored as a double precision value.

class(mypi)
ans =

double



To see more digits, I can now use some of the other format options, for example

format long
mypi
mypi =

3.141592653589793



### Engineering Notation

In Release R14SP2, we enhanced format to include the engineering formats (short and long), resulting in values displayed with powers of 10 appearing in multiples of 3.

format short eng
manypis = mypi*[1 10 100 1000]
manypis =

3.1416e+000    31.4159e+000   314.1593e+000     3.1416e+003



The new formats allow you some extra flexibility on how to view data before resorting to writing your own specialized formatting routines, for example using fprintf.

### Interesting Diversion

While thinking about this post, I wondered about the history of scientific and engineering notation. Using some of my favorite web tools, I found out that scientific notation was first mentioned historically by Florian Cajori who wrote A History of Mathematical Notations (1928-29, 2 vols.).

I next wandered down the hall to talk to Steve of Image Processing blog fame. Wouldn't you know! He sent a letter to his son's teacher last year when the class was asking about what engineering notation was useful for. Steve found some interesting information engineering format for calculators. How cool is it that I can walk into his office and talk about something tangential like this bit of history and find out that not only does the topic interest him too, but he had his own reason at looking into it from a slightly different perspective. It made my day!

### References

Here are some other references that might be helpful for those sorting out questions of display and precision.

### Year End

It's nearly the end of the year 2006 as well as being approximately the first year birthday for this blog. It's been wonderful meeting you in this forum (and occasionally other ways as well!). I'd encourage you to continue sharing your thoughts with my during the next year as well. Happy New Year to all!

Get the MATLAB code

Published with MATLAB® 7.3

Ken Campbell replied on : 1 of 7

I have found all of these blogs particularly interesting and am starting to incorporate some of the tricks I’ve read about it in my own code. I wouldn’t say the end result is particularly slick, but it’s getting better :-) Thanks for your efforts.

Loren replied on : 2 of 7

Ken-

Thanks for the kind words. Here’s to a new year of good code and happy coding!

–Loren

Mohammad Reza replied on : 3 of 7

One the tasks of an array oriented language is to generalise concepts. Take for example the decimal point. In 10.987; it seperate the number into two parts, those parts bigger than one and those that are a fraction. This can be generalised to present the following types of numbers:

complex eg. 1j7 (1+7j)
circular eg. 3p2 (3.5*pi or may be 3*pi^2)
natural eg. 2×3 (2*exp(3))
rational eg. 2r5 (2/5)
base eg. 2b101 (101 in base 2)

and more; imagination is the limit!

The notations can of course be combined, eg:
3r4j5 or 1.2e2r4

just a thought.

/m

RVG replied on : 4 of 7

Thanks for the interesting blog!
I have a question, if I want to display a number
with ONLY 0 in the LHS of decimal point along with ‘e’ precision, how do I do it?
eg. I have 123456 and want to write this into a file
as 0.123456e6, is it possible?
Thanks in anticipation!

Loren replied on : 5 of 7

RVG-

Try various options with format or you might have to use fprintf.

–Loren

Richard replied on : 6 of 7

Is there a way to make fprintf also print numbers in the engineering format? This would be very useful to me as I occasionally use fprintf to print tables of numbers and text for documents.

Thanks,

Richard

These postings are the author's and don't necessarily represent the opinions of MathWorks.