how to change an integer to a natural number

3 visualizaciones (últimos 30 días)
imene. B
imene. B el 18 de Jul. de 2016
Comentada: imene. B el 18 de Jul. de 2016
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 comentarios
James Tursa
James Tursa el 18 de Jul. de 2016
Please show a specific example of the input and desired output.
imene. B
imene. B el 18 de Jul. de 2016
thank you,
for example 8.7 , and i want just to take 8 .

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Jul. de 2016
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 comentario
imene. B
imene. B el 18 de Jul. de 2016
thank you :D ! it worked floor(8.7) gives 8 thank you !

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
John D'Errico el 18 de Jul. de 2016
Editada: John D'Errico el 18 de Jul. de 2016
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 comentarios
John D'Errico
John D'Errico el 18 de Jul. de 2016
floor(8.7) == 8
imene. B
imene. B el 18 de Jul. de 2016
indeed, thank you john

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by