Asked by Natalia Molinero Mingorance
on 18 Mar 2015

Hi,

does anyone know how can I covert 1 in 1.0000, please?

Thank you!

Answer by James Tursa
on 18 Mar 2015

Edited by James Tursa
on 18 Mar 2015

Your question is not clear. If the number is a scalar double and exactly 1, it will display as 1 without the trailing 0's. If it is very close to 1 but not exactly 1, it will display with 0's. E.g.,

>> 1

ans =

1

>> 1+eps(1)

ans =

1.0000

To get more digits to display you can use format. E.g.,

>> format long

>> 1

ans =

1

>> 1+eps(1)

ans =

1.000000000000000

If you really have an integer type and need to convert it to double type, use the double function. E.g.,

>> k = int32(1)

k =

1

>> class(k)

ans =

int32

>> d = double(k)

d =

1

>> class(d)

ans =

double

Natalia Molinero Mingorance
on 20 Mar 2015

Sign in to comment.

Answer by Stephen Cobeldick
on 20 Mar 2015

Edited by Stephen Cobeldick
on 20 Mar 2015

Here is one easy way that you can save a vector of values with your chosen precision: with dlmwrite:

>> A = [-1, 1, 1, -1];

>> dlmwrite('temp.txt',A, 'precision','%.4f');

produces me a file which includes this text:

-1.0000,1.0000,1.0000,-1.0000

Stephen Cobeldick
on 23 Mar 2015

dlmwrite and the similar csvwrite simply write the matrix to a file with exactly the same shape as it has in the supplied variable: so if you give it a 2x3 matrix, it will write a file with two rows and three columns. To get the desired output you just need to rearrange the input matrix to have the desired shape. You can rearrange the shape of an array using reshape, but note that this keeps the columns together:

B = reshape(A,[],2)

If you want to keep the rows together you can transpose before and after the reshape command:

B = reshape(A.',2,[]).';

Note that dlmwrite seems to use unix newlines by default, which means viewing this file using Windows NotePad it will look like there are no newlines: the problem is Notepad, not the file. You can select the 'newline' option 'pc' to change the newline character used.

You can insert spaces by adding a space character into the format specifier string, like this:

dlmwrite('temp.txt',A, 'precision',' %.4f');

which will place a space in front of each number in the file.

Natalia Molinero Mingorance
on 23 Mar 2015

Stephen Cobeldick
on 23 Mar 2015

If you want to control the spaces individually then you will need to use a lower-level function, such as fprintf:

>> A = [-1, 1, 1, -1];

>> fid = fopen('temp.txt','wt');

>> fprintf(fid,' %.4f,%.4f\n',A);

>> fclose(fid);

gives me a file with this inside (there is a space in front of the first column):

-1.0000,1.0000

1.0000,-1.0000

Note that fprintf will simply keep applying that formatting to the input variables until it runs out of variable... and this is columnwise! You might need to transpose the data before inputting it into fprintf.

If this does not resolve your formatting issues, please upload an example file with the exact formatting that you need, and I will show you how to generate it using dlmwrite or fprintf.

Sign in to comment.

Answer by Shantanu Jana
on 23 Mar 2015

Edited by Shantanu Jana
on 23 Mar 2015

you can do this process and latter use 'ans' as variable that hold your answer

>> a=1

a =

1

>>a = double(a)

>> sprintf('%0.4f', a)

ans =

1.0000

>>

Guillaume
on 23 Mar 2015

>>a = 1

creates a of type double. Therefore:

>>a = double(a)

does nothing whatsoever.

Stephen Cobeldick
on 23 Mar 2015

Lets try the suggested code:

>> a = 1;

>> class(a)

ans = double

Lo! we already have a double! Which means that the code suggested by Shantanu Jana:

>> a = double(a);

does nothing useful at all, as a is already of class double.

MATLAB's documentation about floating point numbers states that "...The default is double precision...". My first comment to the original question also clarifies this.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/183859-convert-a-numbere-from-integer-to-double#comment_272968

## Natalia Molinero Mingorance (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/183859-convert-a-numbere-from-integer-to-double#comment_273469

## James Tursa (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/183859-convert-a-numbere-from-integer-to-double#comment_273472

Sign in to comment.