Stop exponential answers
493 次查看(过去 30 天)
显示 更早的评论
Dear all,
I have two values representing the max and min of a data vector.
max_val = 0.9855;
min_val = 0.9851;
Both are of type 'double'. However, when I try to subtract the min from the max:
diff = max_val - min_val;
the answer 3.6023e-04 is returned instead of 0.0004. How can I ensure that this answer is expressed without the exponentiation?
1 个评论
Vishal Singh
2021-2-26
you can use "format short g" command in the start of the code.I am a fresher in matlab but as far as i know it can help to get rid of e in the answer.
采纳的回答
Titus Edelhofer
2011-6-7
Hi,
there is no type in format that generally tells MATLAB to use such a format. Nevertheless you can always use sprintf to make a string in the format you like, in this case it would be
sprintf('%f', diff)
ans =
0.000400
Titus
3 个评论
Guillaume
2016-8-11
format does not alter the result either. It only alters the way it is displayed.
And of course, it is much simpler to change the format to short/long g and display the number as usual than having to call sprintf all the time.
John D'Errico
2016-8-11
I wish we could upvote comments. This one by Guillaume is correct of course. (The answer by Titus is also correct.) But the point is, people seem to think that use of the format command actually changes the number internally. That does not happen. Format only affects the output as it appears in the command window.
更多回答(2 个)
John D'Errico
2011-6-7
I tend to work normally with the display format set as
format short g
for most work. This gives me numbers in a format that I like as often as possible, only going into scientific notation when necessary. Thus
>> format short g
>> X = 3.6023e-04
X =
0.00036023
"short g" is more compact than the alternative of "long g", and most of the time I don't need to see 15 decimal digits in my results.
0 个评论
Robert Cumming
2011-6-7
to make it write the number you could do:
format long g
to actually round your number to the correct number of decimal places you could make an inline function, e.g.:
dcp = inline ( 'round(input.*10.^number)./10.^number' )
dcp(diff,4) % this will round to 4 decimal places
p.s. its not an exponential its engineering format that your answer is expressed as.
2 个评论
John D'Errico
2011-6-7
Note that inline functions are not at all efficient. function handles are far better choices.
dcp = @(inp,number) round(inp.*10.^number)./10.^number;
It is also a poor choice to define a variable with the name input, as this overloads the existing matlab function input.m, preventing you from later using that tool.
Robert Cumming
2011-6-7
it was just a simple example to show what could be done. My actual preference is to have a seperate function which is saved on my path so its always available to me and my team.
As far as using input - I get your point but in simple small functions I dont think its an issue.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!