I have a problem sprintf %f

3 ビュー (過去 30 日間)
Tina
Tina 2013 年 2 月 22 日
Hey!
I have this code:
a=4.5;
str = sprintf('a = %f',a)
str =
a = 4.500000
Could you please tell me how I can modify this so it prints "a=4.5" for me, without having those zeros?

採用された回答

Image Analyst
Image Analyst 2013 年 2 月 22 日
Use fprintf():
a = 4.5;
fprintf('a = %.1f\n', a); % Specify format specifier %.1f to get one decimal place.
Or you could still use sprintf() if you wanted to use the string somewhere else:
a=4.5;
str = sprintf('a = %.1f',a); % Use semicolon, and create a string variable.
fprintf('%s\n', str); % Print string variable to command window.
  1 件のコメント
Tina
Tina 2013 年 2 月 22 日
Thanks :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by