Borrar filtros
Borrar filtros

how do I print number to 1dp - or 0 dp if an integer?

3 visualizaciones (últimos 30 días)
tombola
tombola el 26 de Mzo. de 2012
Hi, I guess the title says it all.
I'm using
MATLAB code
sprintf('%.0f', testval) %to print to 0 dp
sprintf('%.1f', testval) %to print to 1 dp
Is there a way to (simply) print to 0dp if testval is an integer, but 1dp if it has any decimal part?
Thanks a lot,
Tom.

Respuesta aceptada

Wayne King
Wayne King el 26 de Mzo. de 2012
You can use rem()
rem(1.5,1) % gives 0.5
while
rem(3,1) % gives 0
testval = 3.5;
if (rem(testval,1)~=0)
disp('testval has a decimal part');
else
disp('testval is an integer');
end

Más respuestas (0)

Categorías

Más información sobre Additional Math and Discrete en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by