How to remove trailing zeros while display any floting number ?

408 visualizaciones (últimos 30 días)
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...

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Ag. de 2016
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);

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 27 de Ag. de 2016
sprintf('%g',X)

Muhammad Yasirroni
Muhammad Yasirroni el 17 de Jun. de 2019
Editada: Muhammad Yasirroni el 17 de Jun. de 2019
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 comentario
Walter Roberson
Walter Roberson el 20 de Dic. de 2020
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

Iniciar sesión para comentar.


Charles Kluepfel
Charles Kluepfel el 27 de Jul. de 2022
Editada: Walter Roberson el 28 de Jul. de 2022
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 comentarios
Walter Roberson
Walter Roberson el 28 de Jul. de 2022
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 el 28 de Jul. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by