Moving Decimal Places to the right

How can I get the number format as 1412701202.018284 for an exponent format data 1.412701202025964e+09. May be with sprintf "%.6f" or equivalent ? I need to do the task for a mat file I am attaching.

2 Comments

The number format matters for the text output only. In a comment above you mention, that you want to get a double as output. This is not meaningful. So which problem do you want to solve actually?
@Joydeb Saha You need to first respond to the question Jan raised: what is it you want? Do you want a char or a double as the output? In the first case you need sprintf, in the second case your question does not make sense, as that is just how the data is displayed, not the underlying data itself.
What exactly do you want to achieve?

Sign in to comment.

 Accepted Answer

VBBV
VBBV on 29 Sep 2022
Edited: VBBV on 29 Sep 2022
format long % format long option
sprintf('%6f',1.412701202025964e+09)
ans = '1412701202.025964'

9 Comments

Yes it works.
But I need it for an array. Which I attached. If I use
GTT=sprintf('%6f',GT);
I am not getting the result in the double format. Can you please help?
@VBBV: The format long command is not needed here, because it does not influence the sprint command.
@Joydeb Saha: Of course sprintf does not reply a double, but a char. This is the purpose of this command.
If you want the output as a double, there is no need to format the numbers in any way. Formatting concerns the output as characters in the command window only:
a = 1412701202.018284;
b = 1.412701202018284e+09;
isequal(a, b)
ans = logical
1
Internally both numbers are stored exactly the same.
format long
GTT = load('GT.mat')
GTT = struct with fields:
GT: [10×1 double]
GTt = (GTT.GT)
GTt = 10×1
1.0e+09 * 1.412701202018280 1.412701202025960 1.412701202033640 1.412701202041320 1.412701202049000 1.412701202056680 1.412701202064360 1.412701202072040 1.412701202079720 1.412701202087400
fprintf('%6f\n',GTt)
1412701202.018280 1412701202.025960 1412701202.033640 1412701202.041320 1412701202.049000 1412701202.056680 1412701202.064360 1412701202.072040 1412701202.079720 1412701202.087400
try with fprintf
@VBBV: fprintf does exatcly the same as sprintf, except that it writes to a file or the command window, while sprintf catchs the output in a CHAR array.
VBBV, yes it is working. But how can I save save it to workpsace? It is showing in the command window. And it is not showing as a variable in the workspace. My main matrix is 460826x1 in size. So I want it to save after the conversion.
Joydeb Saha
Joydeb Saha on 29 Sep 2022
Moved: Rik on 29 Sep 2022
VBBV, Attached the file which I want to be saved in the desired format I wrote
@Joydeb Saha: As said already, you can store the array in the shown format as CHAR array only:
S = sprintf('%6f\n', GTt)
If you want to store it numerically, the format has no meaning. Matlab stores value in the IEEE754 format and you cannot incfluence the location of the decimal point.

Sign in to comment.

More Answers (0)

Asked:

on 29 Sep 2022

Commented:

Jan
on 29 Sep 2022

Community Treasure Hunt

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

Start Hunting!