How to format output to exponential notation

113 ビュー (過去 30 日間)
Sean St Cyr
Sean St Cyr 2020 年 6 月 29 日
コメント済み: Tommy 2020 年 6 月 29 日
%Additional Data
newName=input('Enter name of new component: ','s');
Name{length(Name)+1}=newName; %adding new component
newCur=input('Enter Current values corresponding to the 5 voltages for the new component in mA:');
CurData=[CurData;newCur]; %appending row of current values for new component
MaxCur=max(max(CurData(2:end,:)));
[R,C]=find(CurData(2:end,:)==MaxCur);
fprintf('Maximum Current = %d mA\n',MaxCur)
fprintf('At voltage = %.3f nV\n',CurData(1,C)*10^9) %%THIS LINE OF CODE IS THE LINE THAT I NEED TO LOOK LIKE X.XXE10
fprintf('\t For %s\n',Name{choice})
Hello! I am trying to format an answer that my code gives out at exponential notation and the line that is labeled is the line of code that i need to edit for it to do so? I have this so far which is giving me the entire number to 10^9 and i need it at X.XXE10. Any suggestions?
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 29 日
Do not use %d for floating point numbers: %d is for integers only.
Sean St Cyr
Sean St Cyr 2020 年 6 月 29 日
I apologize, i forgot to label the line of code, its now labeled, i dont have a %d in that one, it gives me for instance instead of 1.5E10 it gives 15000000000.000

サインインしてコメントする。

採用された回答

Tommy
Tommy 2020 年 6 月 29 日
編集済み: Tommy 2020 年 6 月 29 日
This should work:
fprintf('At voltage = %.2E nV\n',CurData(1,C)*10^9)
If you don't want the plus sign:
val = CurData(1,C)*10^9;
e = floor(log10(val));
b = val / 10^e;
fprintf('At voltage = %.2fE%02d nV\n', b, e);
  2 件のコメント
Sean St Cyr
Sean St Cyr 2020 年 6 月 29 日
It worked, Thank you so much
Tommy
Tommy 2020 年 6 月 29 日
Happy to help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by