I want to change the number's format.
I written
x=[0.00005; 0.0002; 1.00; 0.0015; 0.02; 0.01;];
txt=sprintf('%.e %.4f %.2f %.4f %.2f %.2f',x);
and the result is txt
5.e-05 0.0002 1.00 0.0015 0.02 0.01
But I want to change
x=[0.00005; 0.0002; 1.00; 0.0015; 0.02; 0.01;];
to
x=[5E-005; 0.0002; 1.00; 0.0015; 0.02; 0.01;];
What can I do for this?
Plz help me... ;(

9 commentaires

x = [0.00005; 0.0002; 1.00; 0.0015; 0.02; 0.01;];
txt = sprintf('[%.0e; %.4f; %.2f; %.4f; %.2f; %.2f]',x)
txt = '[5e-05; 0.0002; 1.00; 0.0015; 0.02; 0.01]'
hyein Sim
hyein Sim le 19 Mar 2021
Thank you for response! but I want 0.00005 to 5e-005.
Which have two zero after e ..
How can I do that?
Walter Roberson
Walter Roberson le 19 Mar 2021
What is the purpose of doing that?
What are the rules involved? absolute value less than 1e-4 is to be converted to single digit scientific? Absolute value 1e-4 up to (excluding) 1e-2 is to use 4 digit fixed format? Absolute value 1e-2 (inclusive) to (something?) is to use 2 digit fixed format?
hyein Sim
hyein Sim le 19 Mar 2021
I connect MATLAB and other programs, and the format of substituting numbers into that program is, for example, 8.4e-005 or 5.0e-005. After e, 3 numbers.
So I have to express all numbers in the range 0.00005~0.00015 like that format.
:) Thank toy for your response again..
Walter Roberson
Walter Roberson le 19 Mar 2021
We need rules. How many digits after the decimal place, under what circumstances?
For example how should 0.000314159265358979308997711132889207874541170895099639892578125 be output? How should pi/10000 be output? How should 70, 7, 1/7, 1/70, 1/700, 1/7000 be output ?
hyein Sim
hyein Sim le 19 Mar 2021
Modifié(e) : hyein Sim le 19 Mar 2021
When I put in a number from one to four decimal places at the program(Hydrological program) which I use, such as 0.1 or 0.0004, its are written 0.1 or 0.0004 in the input file of the program. However, if I insert more than 5 decimal places such as 0.00005 or 0.0000074, its are written in input file as 5e-005 or 7.4e-006. Therefore, I change the decimal form because I want to put numbers in that program using matlab. But matlab format is like 5e-05.
You said 0.000314159...number. If I write into the program I use, it will be printed as 0.000314159. Also, I can't write special symbols such as pi/10000 or 1/7 in the program I use.I have to calculate the rest myself and insert it in decimal form. 70 or 7 is output as is. Is this enough for the rules? I'm sorry to bother you, but I've been thinking about it for a week and haven't found the result yet.
hyein Sim
hyein Sim le 19 Mar 2021
So that I just want to know how to change the numbers which range of 0.00005 ~0.00015 to like this 5e-005 format. They will all have 5 decimal places.
Rik
Rik le 19 Mar 2021
So you just want to format the exponent with 3 digits instead of 2? (And use '%.4f' for values larger than 5e-5)
hyein Sim
hyein Sim le 21 Mar 2021
@Rik Yes!! :)

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 19 Mar 2021

0 votes

Please check the output for pi/1000 . Is the rule intended to be "up to 9 decimal places" like it is for 0.000314159, which would give 0.003141592 if you used truncation and 0.003141593 if you used rounding. But perhaps the rule is non-zero plus up to 5 further (so up to 6 decimal places counting from the first non-zero), in which case pi/1000 would give 0.00314159 through rounding or truncation. But if the input value happened to be 0.003141596 and the rule is non-zero+5, then is truncation to 0.00314159 okay, or would you require rounding to 0.00314160 ? And in that case where the trailing digit is not "naturally" 0 but rounded to it, do you want to preserve the trailing 0 or get rid of it, to 0.0031416 ?
Note: I preserved the trailing semi-colon before the ] that you had in your output, in case it was meaningful.
Note: I did not attempt to detect row-vectors and use commas for those, and I did not attempt to deal with 2D arrays.
x = [0.00005; 0.0002; 1.00; 0.0015; 0.02; 0.01; pi/1000; pi/10000];
txt = format_vector(x);
disp(txt)
[5E-005; 0.0002; 1.00; 0.0015; 0.02; 0.01; 0.003141593; 0.000314159;]
function txt = format_vector(vec)
txt = ['[', strjoin(arrayfun(@format_number, vec, 'uniform', 0), ' '), ']'];
end
function str = format_number(num)
%format of each number includes a trailing semi-colon
if abs(num) >= 1e-4
str = sprintf('%.9f;', num);
%Rule: 1 exactly becomes '1.00' not '1.' so preserve at least 2
%decimal places
%Rule: but otherwise trim trailing 0
str = regexprep(str, '0{1,7};$', ';');
else
%Rule: exponent must have three digits
%Rule: trailing 0 after period must be removed
%Rule: if all digits after period are 0, remove period too
%Rule: exponent character must be 'E' not 'e'
str = sprintf('%.5E;', num);
str = regexprep(str, {'0+E', '\.E'}, {'E', 'E'});
str = regexprep(str, {'(E[-+])(\d);$', '(E[-+])(\d\d);$'},{'$100$2;', '$10$2;'});
end
end

1 commentaire

hyein Sim
hyein Sim le 21 Mar 2021
Thank you for kind responses 👍 I will try this and write the results.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by