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

조회 수: 39 (최근 30일)
Chris
Chris 2012년 3월 13일
I have what should be a percentage but it comes up with several decimal places, how can I limit to 2 after the decimal

답변 (2개)

Geoff
Geoff 2012년 3월 13일
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 2012년 3월 13일
Example:
x = 0.78765645; % almost 78.8%
sprintf('%0.2f',x)
ans =
0.79
Is this the effect you're trying to accomplish?
  댓글 수: 2
Tom
Tom 2012년 3월 13일
If it's just for outputs in the command window, you can use
format bank
Image Analyst
Image Analyst 2012년 3월 13일
By the way, the 0 is not necessary. I always just use sprintf('%.2f',x)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by