How to remove decimal??

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 Comment

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?

Sign in to comment.

 Accepted Answer

To make it clear:
a = 2.00000000001
a =
2.0000
Displaying the integer part ( ans is a char), but a retains the fraction:
sprintf('%.f',a)
ans =
2
Dropping the fraction ( ans is same class as a):
fix(a)
ans =
2

More Answers (1)

Fabio
Fabio on 12 May 2012

1 vote

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 Comments

Luisa
Luisa on 12 Nov 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
a = 6.7200
format long g
a
a =
6.72
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
b = 6.7200
round(b,2)
ans = 6.7200
format long g
b
b =
6.72003
round(b,2)
ans =
6.72
If you have a specific output format needed, it is often best to use fprintf() or sprintf() or compose()
Luisa
Luisa on 13 Nov 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))
Error using assert
Assertion failed.
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
output = 10.0000
y_correct = 10
y_correct = 10
isequal(output, y_correct)
ans = logical
0
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
output =
10.00001
format short
output
output = 10.0000
format long g
output
output =
10.00001
fprintf('%.999g\n', output)
10.000009999999999621422830387018620967864990234375
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
Luisa on 19 Nov 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.
Is it possible that @Luisa wanted to vote on @Walter's response?

Sign in to comment.

Categories

Products

Asked:

on 12 May 2012

Commented:

on 20 Nov 2025

Community Treasure Hunt

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

Start Hunting!