MATLAB Answers

Removing the zero that precedes the decimal point

75 views (last 30 days)
z8080
z8080 on 9 Mar 2017
Answered: Jeff Miller on 10 Apr 2018
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!

  0 Comments

Sign in to comment.

Answers (3)

Thorsten
Thorsten on 9 Mar 2017
strrep(num2str(a), '0.', '.')

  3 Comments

z8080
z8080 on 9 Mar 2017
Thanks! Is there also a way to do it with the formatting that I was mentioning?
Stephen Cobeldick
Stephen Cobeldick on 10 Mar 2017
"Is there also a way to do it with the formatting that I was mentioning?"
No.

Sign in to comment.


Dave
Dave on 1 Feb 2018
strrep(sprintf('%3.2f',a),'0.','.')

  0 Comments

Sign in to comment.


Jeff Miller
Jeff Miller on 10 Apr 2018
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')

  0 Comments

Sign in to comment.

Sign in to answer this question.