Borrar filtros
Borrar filtros

How can I format a percent using 2 digits after the decimal

22 visualizaciones (últimos 30 días)
Chris
Chris el 13 de Mzo. de 2012
I have what should be a percentage but it comes up with several decimal places, how can I limit to 2 after the decimal

Respuestas (2)

Geoff
Geoff el 13 de Mzo. de 2012
If you want to do it without using strings:
round(x * 100) / 100;
One reason you might want to do that is if you have a whole matrix of percentages and want to quickly show them in a sensible way, without messing with the output format for other numbers.
-g-

Oleg Komarov
Oleg Komarov el 13 de Mzo. de 2012
Example:
x = 0.78765645; % almost 78.8%
sprintf('%0.2f',x)
ans =
0.79
Is this the effect you're trying to accomplish?
  2 comentarios
Tom
Tom el 13 de Mzo. de 2012
If it's just for outputs in the command window, you can use
format bank
Image Analyst
Image Analyst el 13 de Mzo. de 2012
By the way, the 0 is not necessary. I always just use sprintf('%.2f',x)

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by