How to remove trailing zeros while display any floting number ?
406 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kelvin Viroja
il 27 Ago 2016
Modificato: Walter Roberson
il 28 Lug 2022
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...
0 Commenti
Risposta accettata
Walter Roberson
il 27 Ago 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);
Più risposte (3)
Muhammad Yasirroni
il 17 Giu 2019
Modificato: Muhammad Yasirroni
il 17 Giu 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 Commento
Walter Roberson
il 20 Dic 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
Charles Kluepfel
il 27 Lug 2022
Modificato: Walter Roberson
il 28 Lug 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 Commenti
Walter Roberson
il 28 Lug 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
il 28 Lug 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.
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!