How to remove trailing zeros while display any floting number ?

조회 수: 370 (최근 30일)
Kelvin Viroja
Kelvin Viroja 2016년 8월 27일
편집: Walter Roberson 2022년 7월 28일
format short g;
X=input ('any>');
%if input is 93.93
disp (X);
It display 93.930
How to disp 93.93
I can't remove
format short g;
Bcz of another mathematical reasons and calculations...

채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 27일
The "format short g" statement does not affect calculations at all: it only affects displaying of data.
To display without trailing zeros you should use
fprintf('%g\n', X);

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 27일
sprintf('%g',X)

Muhammad Yasirroni
Muhammad Yasirroni 2019년 6월 17일
편집: Muhammad Yasirroni 2019년 6월 17일
I just found this out. You can remove the trailing zero by using floor.
format short g;
X=input ('any>');
%if input is 93.93
Xnew=floor(X);
disp (Xnew);
It works great. It even can work with matrices and even correcting the value if you want to save it to .mat file.
If you want to retain some value behind the point (.), you can multiply it first.
X=93.33*100;
Xnew=floor(X)/100;
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 12월 20일
floor rounds to negative infinity. floor(-93.93) would be -94. If you wanted -93 instead you would use trunc()
Trailing zeros on output of a numeric item depends on which "format" you currently have in effect, and sometimes also on the other values being displayed. For example
format short
[0 1e20]
is going to display trailing zeroes on the 0

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


Charles Kluepfel
Charles Kluepfel 2022년 7월 27일
편집: Walter Roberson 2022년 7월 28일
This function will convert trailing zeros in a string or character vector, including multiple numbers:
function btz=blankTrailZeros(str)
sout=[char(str) ' '];
trans=false; ppos=0;
for i=1:length(sout)
switch sout(i)
case '.'
trans=true; ppos=i;
case ' '
if trans==true
trans=false;
for j=i-1:-1:ppos
if sout(j)>'0' && sout(j) <='9'
break
end
if sout(j)=='0'
sout(j)=' ';
else
if sout(j)=='.'
sout(j)=' ';
break
end
end
end
ppos=0;
end
end
end
btz=sout(1:end-1);
if isequal(class(str),'string')
btz=string(btz);
end
end
>> a=blankTrailZeros('12.3000 60 5.993 22.0000')
a =
'12.3 60 5.993 22 '
>> blankTrailZeros("12.3300")
ans =
"12.33 "
for use with character strings produced by sprintf.
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 7월 28일
might be easier to use regexprep.
Question about your code: if all digits after the decimal place are 0, is your code stripping the decimal place as well? So 60.0 would become 60 ?
Is there a reason why your code is leaving a variable number of blanks after the last entry even when no blanks originally occurred there?
Charles Kluepfel
Charles Kluepfel 2022년 7월 28일
As in the 22.0000, the decimal point is being stripped as well, so 60.0 would become 60. The 22.0000 has five spaces after it in the result: one for the decimal point that's been removed and four for the four zeros removed. In the font that the description appears in, spaces take up less room than other characters, but every removed character has been replaced by a space, so the length of the output matches that of the input and the decimal points are in the same position for alignment in a column of output.
The 60 in the original was to test, and show, that trailing zeros are not stripped from integers that have zero or a string of zeros all the way to the end.
The code replaces each zero stripped with a space. It doesn't necessarily look like that in the proportional font used for the regular text in the description, but if you copy the test cases to a text editor that uses a fixed width font, like Courier, you'll see there are four spaces between the 12.3 and the 60: one space for each of the three zeros removed and one that was originally there. The whole reason for this is that sprintf can be used rather than fprintf, then use this to transform the result, so that a simple fprintf('%s', result) can be used to keep decimal points aligned in a table of results.

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

카테고리

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