How to adjust the decimal place in fprintf statement

54 ビュー (過去 30 日間)
Katherine
Katherine 2024 年 10 月 28 日 21:03
コメント済み: Les Beckham 2024 年 10 月 31 日 14:44
printf('The maximum force, F(m), on the piling is %1.2f N.\n',Fm);
The value is Fm = 29801.98 N and with this format shown above I only can adjust the number of decimal places it displays.
I am trying to move the decimal 3 places to the left so that it reads out the answer as 29.80 kN. Others answers in my code require different output formats so I need to be able to adjust each in the statement if possible.

採用された回答

Les Beckham
Les Beckham 2024 年 10 月 28 日 21:30
Note that the Matlab function for printing to the command window is fprintf, not printf. Note also that it doesn't make sense to use %1.2f since the 2 decimal places will override the 1 since the 1 is specifying a total field width of 1 but 2 decimal places requires a field width of at least 4, even it the number is less than one. I suggest reading the fprintf documentation more thoroughly (linked above).
Fm = 29801.98;
fprintf('The maximum force, F(m), on the piling is %.2f kN.\n', Fm / 1000); % <<< divide by 1000 to move decimal left 3 places
The maximum force, F(m), on the piling is 29.80 kN.
  2 件のコメント
Katherine
Katherine 2024 年 10 月 28 日 22:00
Thank you! <3
Les Beckham
Les Beckham 2024 年 10 月 31 日 14:44
You are quite welcome.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by