How to remove trailing zeros while display any floating point number ?

353 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 (4)

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;
  2 comentarios
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
DGM
DGM el 3 de Jun. de 2024
Editada: DGM el 3 de Jun. de 2024
The difference between floor() and fix() aside, this demonstrably doesn't do what the question asked to accomplish.
X = 1.1*10;
Xnew = floor(X)/10;
disp(Xnew)
1.1000
X=93.33*100;
Xnew=floor(X)/100;
disp(Xnew)
93.3300
Rounding the number to any given decimal place does not guarantee that it can be exactly represented in floating point, and it doesn't control how it will be displayed, either dumping to console directly with unsuppressed assignment, or by using disp(). You need to treat the number as text in order to have arbitrary control over text display formatting.

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.


Art
Art el 3 de Jun. de 2024
Editada: Art el 3 de Jun. de 2024
"strip" can do this for any amound of decimal places... just change to a str first:
X = 1.31230032000000;
XStr = sprintf('%1.15f', X);
XStripped = strip(XStr,'right','0');
If you're worried about having no trailing zeros after the decimal, a quick "if" statement:
if strcmp(XStripped(end),'.')
XStripped = [XStripped '0'];
end

Categorías

Más información sobre Environment and Settings 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