필터 지우기
필터 지우기

sprintf with leading minus sign

조회 수: 9 (최근 30일)
Sam
Sam 2015년 4월 27일
편집: Stephen23 2016년 8월 5일
I am trying to use SPRINTF to print the following variable's value. How to print it so it does not without the minus sign
>> %This is NOT what I want
>> x=-0.000001; sprintf('%6.2f',x)
ans =
-0.00
>> %This is what I want
ans =
0.00

답변 (3개)

Udit Gupta
Udit Gupta 2015년 4월 27일
use
sprintf('%6.2f',abs(x))

pfb
pfb 2015년 4월 27일
Not sure you can eliminate the minus sign. I think what you are saying is basically
if(abs(x)<1e-2)
x =0;
end

Christian D
Christian D 2016년 8월 5일
I have the same problem, this minus annoys me.
I think a workaround is using the round or roundn on that numbers.
sprintf('%3.0f',-0.1)
sprintf('%3.0f',round(-0.1))
produces
-0
0
Is there any other solution than that?
  댓글 수: 1
Stephen23
Stephen23 2016년 8월 5일
편집: Stephen23 2016년 8월 5일
"a workaround is using the round"
That is not a workaround: round is the way to round numbers. MATLAB supports negative zero because this is required by IEEE 754, which MATLAB uses for floating point values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by