Changing scientific notation to long format
3 views (last 30 days)
Show older comments
Aaron Staszewski
on 5 Aug 2020
Commented: Star Strider
on 6 Aug 2020
Hi all,
I have written the following program to calculate the crosswind for a runway:
function [] = crosswind()
fprintf('\n----- Crosswind -------------------------------------------------- \n');
while 1
% Numerically Defining Input Responses
yes = 1;
Yes = 1;
yEs = 1;
yeS = 1;
YEs = 1;
yES = 1;
YeS = 1;
YES = 1;
y = 1;
Y = 1;
no = 2;
No = 2;
nO = 2;
NO = 2;
n = 2;
N = 2;
%fprintf('-------------------------------------------------------');
%fprintf('\nWelcome to the crosswind component calculator. \n')
%fprintf('This funciton is not a substitute for instructor input. \n');
%fprintf('Input directions in degrees, and velocity in knots. \n\n');
% Asking for Information
runwayDesignator = input('Two digit runway identifier: ');
windDegrees = input('Direction wind is from in degrees: ');
windMagnitude = input('Wind speed in knots: ');
% Computing Information
runwayDegrees = runwayDesignator * 10;
difference = abs( runwayDegrees - windDegrees );
multiplier = sind( difference );
crossWind = abs( windMagnitude * multiplier );
% Printing the Result
if crossWind >= 20
fprintf(2, '\nThere are %d knots of crosswind for runway %d. \n', crossWind, runwayDesignator)
end
if crossWind < 20
fprintf('\nThere are %d knots of crosswind for runway %d \n', crossWind, runwayDesignator)
end
% Asking for further responses / exiting
fprintf('\nWould you like to calculate the crosswind for another runway?\n');
answerA = input('Type response here: ');
if answerA == 2
break;
end
end
end
For example to run the funciton input the numbers:
12
360
20
The funciton works, but I would like the result to be printed in non scientific notation. To do this I tried to insert a like such as:
crossWindLong = sprintf('%2.2f', crossWind);
But when I input this, and run the funciton keeping the input vairables (12, 360 and 20) the same, the function returns some garbage result.
How would I avoid this?
Thanks
2 Comments
Stephen23
on 5 Aug 2020
Rather than defining all of those different "yes" and "no" strings just use the 's' option to return the user string:
str = input(...,'s');
which you can trivially compare using strcmpi:
if strcmpi(str,'no')
"the function returns some garbage result."
>> sprintf('%2.2f', 12)
ans = 12.00
>> sprintf('%2.2f', 360)
ans = 360.00
>> sprintf('%2.2f', 20)
ans = 20.00
dpb
on 5 Aug 2020
Why are you outputting >=20 to standard error (fileID 2 as first argument to fprintf()) and <20 to normal output?
You don't show doing anything with the result -- nor what actual code must have run to get something unexpected.
Why not just replace the "%d" format string in the fprintf calls with the format you would like?
Accepted Answer
Star Strider
on 5 Aug 2020
2 Comments
Star Strider
on 6 Aug 2020
The format setting does not affect fprintf or similar functions. The outputs from them are completely governed by the format string.
With respect to fprintf and the others, use '%.2f' to get two decimal point rounding for all numbers. If you specify a full field width, it must include all decimal digits to the left of the decimal, as well as the decimal itself and the sign. So for example to print π to two decimal places:
fprintf('%.2f', pi)
produces:
3.14
and:
fprintf('%5.2f', pi)
produces:
3.14
(note the leading space for the sign), and:
fprintf('%10.2f', pi)
produces:
3.14
.
More Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!