How do I change the number display from scientific notation to the full number in digits?

2,706 次查看(过去 30 天)
How to make MATLAB output the full number in digits, and not using scientific notation?


Oleg Komarov
Oleg Komarov 2011-8-6
format long
  9 个评论
Walter Roberson
Walter Roberson 2020-4-21
I believe that those are places where the number stored is not the closest representable number to what the value would round to.


更多回答(6 个)

Image Analyst
Image Analyst 2011-8-7
编辑:MathWorks Support Team 2018-11-8
To display the maximum number of digits in a variable without using scientific notation, set the output display format to "longG":
format longG
After you set the display format, variables display in decimal notation:
m = rand(1,3)/1000
m =
0.000546881519204984 0.000957506835434298 0.00096488853519927
To avoid displaying scientific notation for variables that exceed 2^50 use "sprintf". For example, this code displays the number 2332456943534324 in decimal notation:
ans =
For more information, see the "format" documentation:
  2 个评论
Image Analyst
Image Analyst 2011-8-7
Yes it can help. Sometimes some sneak through even with that (if there would be more than three 0's to the right of the decimal point), like this which I tried:
m =
Columns 1 through 4
0.000538342435260057 0.000996134716626886 7.81755287531837e-005 0.000442678269775446
Columns 5 through 8
0.000106652770180584 0.000961898080855054 4.63422413406744e-006 0.000774910464711502


Kaveh Vejdani
Kaveh Vejdani 2018-2-19
I don't understand why you have accepted the wrong answers. What you're looking for is: format short g
Cheers, Kaveh
  3 个评论
Walter Roberson
Walter Roberson 2018-2-19
>> format short g
>> pi
ans =
This is not "full number in digits"
>> 1000000
ans =
this is not even close to being an "absurdly huge number"
format short g gives you at most 5 significant figures.
format long g gives you at most 15 significant figures. It turns out that is not enough in practice to be unique. There are 24 distinct representable values in unique(pi-37*eps:eps:pi+9*eps), all of which display as 3.14159265358979 under format long g. If the goal is to output enough digits to be able to transfer the values exactly in text form, then format long g is not sufficient.
People get caught by this all the time!
format long g
T = 0.3 - 0.2
T == 0.1
T - 0.1
T =
ans =
ans =
People have difficulty understanding why a value that shows up as 0.1 does not compare as equal to 0.1: the limits of format long g have real effects.


Mark Bower
Mark Bower 2017-10-20
编辑:Mark Bower 2017-10-20
A nice, consistent solution is to use "num2str()". The same call works for both display from the command line:
> val = 1234567890
val =
> num2str(val)
ans =
and also within print statements:
> sprintf(num2str(val))
ans =
It also works for floating point numbers:
> val = 123456.789
val =
> sprintf(num2str(val))
ans =
  2 个评论
Stephen23 2018-2-20
编辑:Stephen23 2018-2-20
The sprintf is totally superfluous, it does nothing useful at all here, just slows down the code. In any case, using a proper sprintf format string would be quicker than calling num2str, and provide more control over the number of digits, so why not do that?


Walter Roberson
Walter Roberson 2018-2-19
For MS Windows and Linux, to get full number of digits and not in exponential form, you need to either use the Symbolic toolbox or you need to use a tool such as from the File Exchange. This is crucial for MS Windows, which does a rather poor job of converting exact values; Linux does a better job but still has inaccuracies after a while.
On Mac (OS-X, MacOS), the built in conversion is exact, and you can choose to sprintf() with a '%.1074f' format. For example,
>> sprintf('%.1074f', eps(realmin))
ans =
For larger values you might want to trim out trailing zeros from the converted string
val = pi*1E-200;
regexprep( sprintf('%.1074f', val), '0+$', '', 'lineanchors')
ans =

Huw S
Huw S 2017-1-31
If you don't need to know all the decimal points, then do your equation inside round.
saves all the other bother of exponentials.
  1 个评论
Walter Roberson
Walter Roberson 2017-1-31
Unfortunately not the case:
>> format short
>> round(2^54)
ans =
>> format long g
>> round(2^54)
ans =
>> uint64(2^54)
ans =


Christos Boutsikas
Christos Boutsikas 2020-4-21
You can also use Variable-precision arithmetic via command vpa.
vpa(x) %if x is the output number you are interesting in


Help CenterFile Exchange 中查找有关 Tables 的更多信息


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by