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

 Accepted Answer

format long or sprintf('%16.f',2332456943534324)

9 Comments

i have tried this but its not showing the complete value. i have tried below:
Code:
format long
pmod=hex2dec('DB7C2ABF62E35E668076BEAD208B');
fprintf('\n%.f',pmod);
Output :
4451685225093714900000000000000000
but the correct output should be
4451685225093714772084598273548427
Is there any other method to get the complete value?
You can't exactly represent that number in double precision floating point. When you call the eps function with a numeric input, it returns the distance to the next largest floating-point number. In your case:
>> format longg
>> eps(4451685225093714772084598273548427)
ans =
5.76460752303423e+17
So representable numbers are widely spaced when you get to that large a magnitude, and there's no guarantee that the particular number you want is one of the representable numbers. See the picture on the first page of this Cleve's Corner article, but imagine if the numbers on the X axis were MUCH larger and the red lines MUCH more widely spaced.
So how could you do this? Use Symbolic Math Toolbox and be very careful to use symbolic arithmetic, not double precision arithmetic, when converting your string of hex digits into a symbolic value.
How do I change from scientifc number to full number in digits after I do an import from excel? I see scientific number in the table? I want to see full number?
None of the display formats show all of the digits. "long g" format comes closest but is 1 digit short of uniquely identifying the stored binary. You will need to use fprintf to request 16 digits. If you are using MS Windows then you cannot count on fprintf for this purpose and you should look in the File Exchange for num2strexact
If you are using the variable browser then at the top of the area you have the ability to change the format for that one variable (until you have close that browser on the variable), or you can look in Preferences to adjust the default used by the variable browser. Again, you will not be able to see the last meaningful digit this way.
I am interested in how I set this format when I import data from excel into a table in Matlab I see the scientific value, I want to see the full number value in the table? your method works for the output values.
data.png
I changed the format short fixed decimal.
data2.png
but it still showed the scientifc number.
You would need to hack the Mathworks table object display code in order to get the table to display all 16 digits.
You should extract the values from the table and use fprintf with them in order to see the full 16 digits.
Question: when you say "the full number" then do you mean that if you had a value such as 1e25 stored then you would want to see the display as 10000000000000000000000000?
Those scientific notation are places where there are additional decimal places not just integers. You will not be able to see the fraction with any of the "short" choices and will need to switch to the "long" choices.
no matter what number format I display, it still does not show me the full number that was present on the excel file?
I believe that those are places where the number stored is not the closest representable number to what the value would round to.

Sign in to comment.

More Answers (6)

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:
sprintf('%16.f',2332456943534324)
ans =
'2332456943534324'
For more information, see the "format" documentation:

2 Comments

format long g
helps. However, integers that exceed 2^53 will be represented in scientific notation with "format long g". To get the full digits of those, you need to use sprintf() or fprintf()
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

Sign in to comment.

I don't understand why you have accepted the wrong answers. What you're looking for is: format short g
Cheers, Kaveh

3 Comments

>> format short g
>> pi*10^5
ans =
3.1416e+05
That does not appear to satisfy either part of the requirement,
"how to make matlab output the full number in digits, and not in exponential form?"
For any formatting, one can find a special case (an absurdly huge number or an infinitesimally small one) to make it fail. For "practical" purposes, long g and short g will do the job perfectly.
>> format short g
>> pi
ans =
3.1416
This is not "full number in digits"
>> 1000000
ans =
1e+06
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 =
0.1
ans =
logical
0
ans =
-2.77555756156289e-17
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.

Sign in to comment.

A nice, consistent solution is to use "num2str()". The same call works for both display from the command line:
> val = 1234567890
val =
1.234567890000000e+09
> num2str(val)
ans =
1234567890
and also within print statements:
> sprintf(num2str(val))
ans =
1234567890
It also works for floating point numbers:
> val = 123456.789
val =
1.234567890000000e+05
> sprintf(num2str(val))
ans =
123456.789
>

2 Comments

>> num2str(pi*10^5)
ans =
'314159.2654'
This is not "full decimal places"
Using num2str() inside sprintf() is redundant.
sprintf(num2str(val))
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?

Sign in to comment.

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 https://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact--exact-version-of-num2str- 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 =
'0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625'
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 =
'0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003141592653589793111936498419027683964072757959391149845317813416927695644722162706379483043156554579881967829022575831926635177847590589777088086173081089243142930507159490615800591052996089483276727788901006686618108987452642387169053033459820326372299902201815389727889699071056417123601253516892437642498120285079407325647552658339885701180059456745257476645670329996938769926310811984167666114826593537757304481509915842491117931968666219637406979734598283259283758102504979792257699955371208488941192626953125'
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 Comment

Unfortunately not the case:
>> format short
>> round(2^54)
ans =
1.8014e+16
>> format long g
>> round(2^54)
ans =
1.8014398509482e+16
>> uint64(2^54)
ans =
uint64
18014398509481984

Sign in to comment.

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

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!