How to round a result to two decimal places
Mostrar comentarios más antiguos
r=10 (4/3)*pi*r^3 Round the result to two decimal places
1 comentario
Sarath J
el 22 de Nov. de 2017
method 1: round(r)
method 2: format bank; r
Respuesta aceptada
Más respuestas (3)
Kirby Fears
el 24 de Oct. de 2016
Mo'men,
Check out the round function.
r_rounded = round(r,2); % rounds to 2 decimal places
Hope this helps.
5 comentarios
dpb
el 24 de Oct. de 2016
I was thinking there was something but didn't lookfor it to remind myself...much better as it's a builtin and general...
Miquel
el 12 de En. de 2020
The solution is good, but it keeps the zeros after the rounding. So, if the number was 2.3789 it is now 2.3800. I need to copy matrices and paste them in tables of a Word document, so I need the rounded numbers without the zeros. Is there a way to be able to get rid of the zeros to do the copy paste?
dpb
el 12 de En. de 2020
As another said, if it's precisely two decimal places you're wanting to keep, then format bank will fix the display in command window.
If your output is to something else, then use a specific format string and write to file or use the COM stuff and stuff directly into Word.
Miquel
el 13 de En. de 2020
Thanks for the mega-fast answer!
In principle I wanted to copy the matrices from the Variables window in MATLAB. I tried to copy them from the command window with the short g format, as you suggest, but then it is like copying a bunch of text and I don't manage that it gets pasted correctly in the table.
What do you mean with the COM stuff in Word?
Thanks
Eleanor Betton
el 11 de Nov. de 2020
You can write to word with the report generator function. I used this for rounding a table to go into this:
M = round(V*100)/100;
C = num2cell(V);
fun = @(x) sprintf('%0.2f', x);
V = cellfun(fun, C, 'UniformOutput',0);
V=array2table(V);
Thabang Kekana
el 18 de Jun. de 2021
0 votos
12.22
luke
el 29 de Sept. de 2022
0 votos
myAns = 2 * (-4-[6+3+(7-(1+8))+12]-3)+5
myAns =
-47
Round the result to two decimal places
Categorías
Más información sobre Scope Variables and Generate Names en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!