0

I am dividing a number, say

x=2;
y=1/3;
z=x*y;

I expect z to be 0.66666666666667 i.e. 14 numbers after decimal point and the same for y. Now when I do that I get 0.6667 only. How to expand the answer into exact 14 digit precision?

darthbith
  • 18,484
  • 9
  • 60
  • 76
MSD
  • 109
  • 1
  • 2
  • 12

3 Answers3

7

If it is just about diplaying the number you can use the sprintf function

str = sprintf('%.14f', pi); % print PI with exactly 14 digits after decimal point
disp(str);

Or fprintf with just one line:

fprintf('%.14f', pi);

fprintf is for writing to files but prints the string result to the screen if no file identifier is given.

Deve
  • 4,528
  • 2
  • 24
  • 27
6

If you actually want the number to have that precision then round off to 14 places like this:

format long g

x=2;
y=1/3;
z=x*y;
round(z*1e14)/1e14

ans =      0.66666666666667

If it's just about displaying then use Deve's sprintf solution.

Dan
  • 45,079
  • 17
  • 88
  • 157
1

you can type in matlab :

digits(10)

it gives you more floating point precision, or also

digits(20)

for example:

 a=1/3
ans = 0.3333
digits(10)
 vpa(a)
 ans= 0.3333333333

search help of matlab by typing doc in command window and then search : digits

Pang
  • 9,564
  • 146
  • 81
  • 122
zizi
  • 11
  • 1
  • oops, i mean digits(10) – zizi Feb 17 '15 at 02:04
  • 1
    Using the Variable Precision Arithmetic module is an ass-backwards (no offence) way of showing more digits of precision if you only require a small amount of digits to be shown. Only use `vpa` if you require much larger amount of digits of precision. In this particular case, `vpa` is not required here. – rayryeng Dec 04 '15 at 19:33