Removing the zero that precedes the decimal point

조회 수: 38 (최근 30일)
z8080
z8080 2017년 3월 9일
답변: Jeff Miller 2018년 4월 10일
I would like to have numbers between 0 and 1 be displayed such as .64 instead of 0.64, i.e. omitting the initial zero.
Since the total number of chars including the decimal point would then be 3, with 2 decimals, I tried
num2str(a,'%3.2f')
as well as
sprintf('%3.2f',a)
(as well as several variations for each), but unexpectedly, they both leave the leading zero before the decimal.
What is the correct formatting tag? Thanks!

답변 (3개)

Thorsten
Thorsten 2017년 3월 9일
strrep(num2str(a), '0.', '.')
  댓글 수: 3
Thorsten
Thorsten 2017년 3월 10일
Sorry, I don't know.
Stephen23
Stephen23 2017년 3월 10일
"Is there also a way to do it with the formatting that I was mentioning?"
No.

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


Dave
Dave 2018년 2월 1일
strrep(sprintf('%3.2f',a),'0.','.')

Jeff Miller
Jeff Miller 2018년 4월 10일
Here is a little follow-up in case anyone else has this problem...
I had the same problem as z8080, and I wanted to solve it with a format statement that I could pass to xtickformat or ytickformat. My solution is ugly, but it works:
num2str(100*a,'.%02d')
where the values of 'a' are in the 0--1 range. Or, in my case:
plot(x,100*y,'-V')
ytickformat('.%02d')

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by