Rounding a decimal down
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
L'O.G.
el 2 de Dic. de 2022
Comentada: Emre Tas
el 8 de Ag. de 2023
With round(x,2) I can round a number to the nearest hundredth, but how do I round down to the nearest hundredth? For example, both 0.143 and 0.147 should become 0.14.
0 comentarios
Respuesta aceptada
Más respuestas (2)
Vilém Frynta
el 2 de Dic. de 2022
Editada: Vilém Frynta
el 2 de Dic. de 2022
Example:
X = 0.143;
Y = sprintf('%.2f',X);
Y = str2double(Y)
2 comentarios
Stephen23
el 3 de Dic. de 2022
Editada: Stephen23
el 3 de Dic. de 2022
It is more efficient to keep this in the numeric domain, rather than converting to text and back.
It also does not work, because SPRINTF rounds values to the nearest digit, it does not round down:
X = 0.147;
Y = sprintf('%.2f',X);
Y = str2double(Y) % Nope, definitely not rounded down.
The answers from Matt J and Les Beckham are correct.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!