Borrar filtros
Borrar filtros

Keeping a zero after a decimal

101 visualizaciones (últimos 30 días)
Andre Dixon
Andre Dixon el 6 de Dic. de 2017
Comentada: Stephen23 el 14 de Feb. de 2023
Hi! I'm relatively new to MATLAB and am trying to round the values within this column vector to three significant digits. This has worked fine except for the second entry, which fails to keep a zero after the decimal point when rounded. If I have a vector: A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256], no matter whether I use: result = round(A, 3, 'significant') or result = round(A, 1), both produce the following: 27.4; 25; 28.9; 29.2; 23.8; 27.3. I would like the second row to be 25.0. Any help is much appreciated! Cheers :)
  7 comentarios
Kurt
Kurt el 13 de Feb. de 2023
Editada: Kurt el 13 de Feb. de 2023
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256]
A = 6×1
27.3720 24.9660 28.9080 29.2050 23.7830 27.2560
display(sprintf('%0.1f\n',round(A,1)));
27.4 25.0 28.9 29.2 23.8 27.3
Stephen23
Stephen23 el 14 de Feb. de 2023
@Kurt: SPRINTF() also performs rounding, so the separate ROUND() call is not required:
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256];
fprintf('%0.1f\n',A)
27.4 25.0 28.9 29.2 23.8 27.3

Iniciar sesión para comentar.

Respuestas (1)

Christopher Coello
Christopher Coello el 7 de Feb. de 2018
Editada: Christopher Coello el 7 de Feb. de 2018
Well not sure about the question. But if what you want is display the vector with trailing zero then you can use the undocumented sprintfc command
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256];
% with one decimal digit
sprintfc('%0.1f',A)
% with two decimal digits
sprintfc('%0.2f',A)
% with five decimal digits
sprintfc('%0.5f',A)
  6 comentarios
Christopher Coello
Christopher Coello el 7 de Feb. de 2018
Nice, I like compose ! Thanks Guillaume, I'll definitely drop sprintfc now
Elysi Cochin
Elysi Cochin el 14 de Oct. de 2022
Thanks @Guillaume

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by