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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by