How to remove decimal??

63 views (last 30 days)
baby
baby on 12 May 2012
Commented: Luisa ongeveer 6 uur ago
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
Oleg Komarov
Oleg Komarov on 12 May 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?

Sign in to comment.

Accepted Answer

Oleg Komarov
Oleg Komarov on 12 May 2012
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
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
  6 Comments
Walter Roberson
Walter Roberson ongeveer 22 uur ago
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 ongeveer 6 uur ago
@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.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!