how to change an integer to a natural number

조회 수: 8 (최근 30일)
imene. B
imene. B 2016년 7월 18일
댓글: imene. B 2016년 7월 18일
hi all, i have a function answer witch is an integer number, but i want to ignore the numbers after the coma and to take the natural part, how to do that ?
Thanks.
  댓글 수: 2
James Tursa
James Tursa 2016년 7월 18일
Please show a specific example of the input and desired output.
imene. B
imene. B 2016년 7월 18일
thank you,
for example 8.7 , and i want just to take 8 .

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 18일
You need either floor() or fix() . The two differ in their handling of negative values. floor(-8.7) would be -9, and fix(-8.7) would be -8
  댓글 수: 1
imene. B
imene. B 2016년 7월 18일
thank you :D ! it worked floor(8.7) gives 8 thank you !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2016년 7월 18일
편집: John D'Errico 2016년 7월 18일
An integer already IS a natural number.
https://en.wikipedia.org/wiki/Natural_number
So, WOW! You already did what you wanted.
If you are seeing multiple zeros AFTER the decimal place reported, then your number is not in fact an integer. Actually, you must have some floating point trash in there, that causes MATLAB to show those zeros.
N = 1 + eps
N =
1.000000000000000
As you can see, MATLAB knows that it is not really exactly 1. It reports all those sparse zeros, because MATLAB see trash out there past the digits that it reports.
N == 1
ans =
0
Now lets use round.
N = round(N)
N =
1
So, now N really, truly is an integer, a natural number. MATLAB knows that, and displays it with no trailing zeros.
N == 1
ans =
1
  댓글 수: 3
John D'Errico
John D'Errico 2016년 7월 18일
floor(8.7) == 8
imene. B
imene. B 2016년 7월 18일
indeed, thank you john

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by