How to remove decimal??
Mostrar comentarios más antiguos
Hello,,
i wanna know how to remove decimal??
Example a = 2.0000
how to remove zero beside point so it will be a = 2??
please help me
1 comentario
Oleg Komarov
el 12 de Mayo de 2012
Note that a is most likely not exactly 2, so the question is, do you want to "display" only the integer part or do you want to "drop" the decimal?
Respuesta aceptada
Más respuestas (1)
Fabio
el 12 de Mayo de 2012
1 voto
As Oleg says if you want to 'display' the integer part just cast to String with num2Str() function: http://www.mathworks.it/help/techdoc/ref/num2str.html
If you want to drop the decimal part you can use round() function: http://www.mathworks.it/help/techdoc/ref/round.html
8 comentarios
Luisa
el 12 de Nov. de 2025
Thank you. It worked for me. I needed to rewrite a = 6.7200 as 6.72.
Note that when 6.7200 is displayed, chances are that you have "format short" in effect, and changing the format would change how it is displayed
a = 6.7200;
format short
a
format long g
a
If you have format short in effect, then although you can round(), the format short puts the display back to 4 decimal places.
b = 6.72003;
format short
b
round(b,2)
format long g
b
round(b,2)
If you have a specific output format needed, it is often best to use fprintf() or sprintf() or compose()
Luisa
el 13 de Nov. de 2025
Thank you so much.
@Walter Roberson Sorry, I still do not understand why it works:
output = [9.7108 2.4028 23.3333];
y_correct = [9.710847 2.402811 70/3];
tolerance = 1e-6;
assert(all(abs(output-y_correct)<tolerance))
and it fails:
output = [10.2285 2.3464 24.0000];
y_correct = [10.228550 2.346386 24];
tolerance = 1e-6;
assert(all(abs(output-y_correct)<tolerance))
And how should i fix altogether with above answers:
output = [10.0000 2.0000 20.0000];
y_correct = [10 2 20];
assert(isequal(output,y_correct))
Thank you in advance.
9.7108 - 9.710847 is -0.000047 . abs() of that is 0.000047 . That is not less than 1e-6 so the assertion fails.
10.2285 - 10.228550 is -0.000050 . abs() of that is 0.000050. That is not less than 1e-6 so the second assertion fails.
10.0000 is just another way to write 10, and likewise 2.000 is another way to write 2 and 20.0000 is another way to write 20, so all of the isequal() succeed, so the assertion passes.
Now, it is possible that what you really have is
format short
output = 10.00001
y_correct = 10
isequal(output, y_correct)
In this case, output displays as 10.0000 but that is only the display .The choice of format does not affect what is actually stored.
format long g
output
format short
output
format long g
output
fprintf('%.999g\n', output)
Observe there is no change in the value stored, just changes in how it is displayed . Each time the actual internal value stored for 10.00001 is 10.000009999999999621422830387018620967864990234375 and format chooses between various output representations.
Note: there is no format that displays all of the internal representation; you need fprintf() or sprintf() or compose() for that.
Luisa
el 19 de Nov. de 2025
@Walter Roberson Yes, I discovered the discrepancies with your suggestion:
fprintf('%.999g\n', output)
First example worked, because the output had indeed more than 4 decimal digits
and they matched the values of y_correct in the tolerance of 1e-6.
Second example failed, because although the output also had more than 4 decimal digits,
the y_correct was rounded and did not respect the tolerance of 1e-6.
Last example, the computador wrongly calculated the first output by
9.9999999999999982236431605997495353221893310546875
instead 10.0000, as you said.
Thank you so much.
@Cody Team, how can I vote or give a like in great comments?
It seems that there are not such options.
Thank you in advance.
Walter Roberson
el 19 de Nov. de 2025
I created a Discussion for your idea about cody voting; https://www.mathworks.com/matlabcentral/discussions/ideas/885295-cody-comment-voting-idea?success=true
Dyuman Joshi
el 20 de Nov. de 2025
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!