Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fprintf and insignificat zeros

Subject: fprintf and insignificat zeros

From: Øyvind

Date: 5 Sep, 2008 09:14:03

Message: 1 of 10

Is there any way to get fprintf to suppress insignificant
trailing zeros with fixed-point notation?

'%g' will give scientific notation if that is the most
compact, but that is not acceptable for my use.

E.g.
>> sprintf('%g',0.0000023000)

ans =

2.3e-006


I need it to output '0.0000023', but I do not know in
advance the magnitude of the number, so variants of '%f'
doesn't help either.

Thanks.
oyvist

Subject: fprintf and insignificat zeros

From: Steve Amphlett

Date: 5 Sep, 2008 09:25:04

Message: 2 of 10

"?yvind " <oyvist@gmail.com> wrote in message <g9qt8q$7f7
$1@fred.mathworks.com>...
> Is there any way to get fprintf to suppress insignificant
> trailing zeros with fixed-point notation?
>
> '%g' will give scientific notation if that is the most
> compact, but that is not acceptable for my use.
>
> E.g.
> >> sprintf('%g',0.0000023000)
>
> ans =
>
> 2.3e-006
>
>
> I need it to output '0.0000023', but I do not know in
> advance the magnitude of the number, so variants of '%f'
> doesn't help either.


How many insignificant trailing zeros are there here:

>> sprintf('%.20f\n',0.1)

ans =

0.10000000000000001000


It's not quite as simple as it may seem.

Subject: fprintf and insignificat zeros

From: &#xD8;yvind

Date: 5 Sep, 2008 10:54:01

Message: 3 of 10

> How many insignificant trailing zeros are there here:
>
> >> sprintf('%.20f\n',0.1)
>
> ans =
>
> 0.10000000000000001000
>
>
> It's not quite as simple as it may seem.


There are 3 trailing zeros. I am talking about trailing
zeros in the *output string*, not about the precision
problems inherent in floating point numbers. '%g' handles
this the way I want it to:

>> sprintf('%.20g',0.1)

ans =

0.10000000000000001

I am looking for some way to do what '%g' does, *except* for
the scientific format output it sometimes produces.

oyvist

Subject: fprintf and insignificat zeros

From: Steve Amphlett

Date: 5 Sep, 2008 11:08:01

Message: 4 of 10

"?yvind " <oyvist@gmail.com> wrote in message <g9r349
$pdp$1@fred.mathworks.com>...
> > How many insignificant trailing zeros are there here:
> >
> > >> sprintf('%.20f\n',0.1)
> >
> > ans =
> >
> > 0.10000000000000001000
> >
> >
> > It's not quite as simple as it may seem.
>
>
> There are 3 trailing zeros. I am talking about trailing
> zeros in the *output string*, not about the precision
> problems inherent in floating point numbers. '%g' handles
> this the way I want it to:
>
> >> sprintf('%.20g',0.1)
>
> ans =
>
> 0.10000000000000001
>
> I am looking for some way to do what '%g' does, *except*
for
> the scientific format output it sometimes produces.
>
> oyvist


So you want to clone <deblank>, but use '0' instead of
white space?

Something Friday-ish like:

a=sprintf('%f', 0.123)
a(1:length(a)+1-find(fliplr(a)~='0',1,'first'))

Subject: fprintf and insignificat zeros

From: us

Date: 5 Sep, 2008 11:10:19

Message: 5 of 10

"?yvind ":
<SNIP wants to remove trailing zeros...

> >> sprintf('%g',0.0000023000)
> ans =
> 2.3e-006
> I need it to output '0.0000023', but I do not know in
> advance the magnitude of the number, so variants of '%f'
> doesn't help either...

one of the solutions

     n=0.0000023;
     ns=sprintf('%.100f',n);
     ns=ns(1:regexp(c,'[^0]0+$'))
% ns = 0.0000023

us

Subject: fprintf and insignificat zeros

From: us

Date: 5 Sep, 2008 11:24:02

Message: 6 of 10

"?yvind ":
<SNIP wants to remove trailing zeros...

> >> sprintf('%g',0.0000023000)
> ans =
> 2.3e-006
> I need it to output '0.0000023', but I do not know in
> advance the magnitude of the number, so variants of '%f'
> doesn't help either...

one of the solutions

     n=0.0000023;
     ns=sprintf('%.100f',n);
     ns=ns(1:regexp(ns,'[^0]0+$')) % <- TYPO in last POST
% ns = 0.0000023

(sorry for typo in last post)
us

Subject: fprintf and insignificant zeros

From: &#xD8;yvind

Date: 5 Sep, 2008 12:32:01

Message: 7 of 10

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <g9r3uh$2r5$1@fred.mathworks.com>...
> So you want to clone <deblank>, but use '0' instead of
> white space?
>
> Something Friday-ish like:
>
> a=sprintf('%f', 0.123)
> a(1:length(a)+1-find(fliplr(a)~='0',1,'first'))
>

Thanks for the suggestions, Steve and us. Unfortunately it's a bit more difficult than this. I'm writing strings that contain a mixture of numbers and characters. Typical example:

fprintf(fid,
'\\draw[shift={(%g,%g)},xstep=%g,ystep=%g,thin,%s] (P0) grid +(\\width,\\height);\n\n', ...
xshift,yshift,abs(diff(xtick(1:2))),abs(diff(ytick(1:2))),gridstyle);

Based on what you suggested I have a sort of a fix:

>> str = 'Hello 0.003000, 0. testing 1,2,3.';
>> regexprep(str,'(\d+\.\d+?)0*\>','$1')

ans =

Hello 0.003, 0. testing 1,2,3.


Problem is, I have a huge function with maybe hundreds of fprintf statements, and now each of them will require three statements instead of one. What I was really looking for was some way of doing it all inside the fprintf. There isn't any way of fiddling with the conversion characters that will produce the result I prefer, is there?

Seems like there ought to be a conversion like this.

oyvist

Subject: fprintf and insignificant zeros

From: us

Date: 5 Sep, 2008 12:55:04

Message: 8 of 10

"?yvind ":
<SNIP fprintf-mess...

> Problem is, I have a huge function with maybe hundreds of fprintf statements, and now each of them will require three statements instead of one...

suggestion:

- in your function(s): replace all <fprintf> with <mprintf>
- create the subfun <mprintf> with the necessary conversion
  steps
  function txt=mprintf(varargin);
% rex/fprintf-stuff
- note: this is very flexible in case requirements change...

just a thought
us

Subject: fprintf and insignificant zeros

From: &#xD8;yvind

Date: 5 Sep, 2008 13:27:01

Message: 9 of 10

"us " <us@neurol.unizh.ch> wrote in message <g9ra78$2hj$1@fred.mathworks.com>...
> suggestion:

Actually that's a good suggestion. I thought of it before but could not get it to work the way I wanted, for some reason I don't quite get now.

This time I changed all '%g'-conversions to '%f' and added the subfunction

function fprintf(fid,varargin)
   str = regexprep(sprintf(varargin{:}),'(\d+\.\d+?)0*\>','$1');
   builtin('fprintf',fid,'%s',newstr);
end

which gives minimal rewrites. Thanks a lot.

Still, if someone knows about a way to do this with fprintf-flags etc. it would be very welcome. I really think there should be some built-in conversion like this.

oyvist

Subject: fprintf and insignificant zeros

From: Bashir Souid

Date: 22 May, 2009 14:13:03

Message: 10 of 10

"?yvind " <oyvist@gmail.com> wrote in message <g9rc35$nra$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <g9ra78$2hj$1@fred.mathworks.com>...
> > suggestion:
>
> Actually that's a good suggestion. I thought of it before but could not get it to work the way I wanted, for some reason I don't quite get now.
>
> This time I changed all '%g'-conversions to '%f' and added the subfunction
>
> function fprintf(fid,varargin)
> str = regexprep(sprintf(varargin{:}),'(\d+\.\d+?)0*\>','$1');
> builtin('fprintf',fid,'%s',newstr);
> end
>
> which gives minimal rewrites. Thanks a lot.
>
> Still, if someone knows about a way to do this with fprintf-flags etc. it would be very welcome. I really think there should be some built-in conversion like this.
>
> oyvist

i was looking for exactly this, thank you all. just one issue:

>> regexprep(sprintf('%.100f',100.000),'(\d+\.\d+?)0*\>','$1')

ans =

100.0

for some reason it is 100.0 not 100. if i use 100 instead of 100.000 it will still be 100.0.

any way to fix this? thanks.

i also noticed some errors for some values but it might be related to FP representation and its inherit error.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us