Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: valerasimonov@gmail.com Newsgroups: comp.graphics.apps.gnuplot Subject: Re: How to print floats with leading blanks instead of leading zeros using sprintf? Date: Mon, 28 May 2012 20:19:58 -0700 (PDT) Organization: http://groups.google.com Lines: 82 Message-ID: References: <3d97629a-6d13-4e42-bb08-9a1d05a569d9@vy7g2000pbc.googlegroups.com> NNTP-Posting-Host: 132.206.126.41 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1338261710 21223 127.0.0.1 (29 May 2012 03:21:50 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 29 May 2012 03:21:50 +0000 (UTC) Cc: sfeam@users.sourceforge.net In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=132.206.126.41; posting-account=4Nx8HQoAAACPrCgQ4Rd61O9cDhaTWCLL User-Agent: G2/1.0 X-Received-Bytes: 3850 Xref: csiph.com comp.graphics.apps.gnuplot:1155 On Monday, May 28, 2012 2:16:26 PM UTC-4, sfeam wrote: > =D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D0=B9 =D0=A1=D0=B8=D0=BC=D0=BE=D0=BD= =D0=BE=D0=B2 wrote: >=20 > > Hi everyone, > >=20 > > I want to specify labels for the plots using sprintf. > > According to > > http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ sprintf(' > > %3.0f',1) should print "001" > > sprintf(' %3.0d',1) should print " 1". > > However gnuplot outputs: > > sprintf(' %3.0f',1) "001" > > sprintf(' %3.0d',1) "1" (without leading blanks). > >=20 > > Does anyone know how to fix this issue? >=20 > I cannot reproduce either of those results here. > The first one in particular makes no sense. > Could it be that you are not really using the format you show us? >=20 > gnuplot> print sprintf("|%3.0d|",1) > | 1| > gnuplot> >=20 >=20 > >=20 > > Thanks in advance! > >=20 > > Regards, > > Valera sfeam, thank you for your reply. I will provide some more details. I want to plot the file: "sprintf.txt" 0.2 1.5970E-01 1.1262E-01 0.4 5.7188E-01 2.2227E-01 0.6 2.2019E-01 4.3648E-01 executing the following script: ./plot_sprintf 20 1 #!/bin/sh gnuplot << EOF set terminal postscript eps enhanced set output 'plot_sprintf.eps' plot "sprintf.txt" using (\$1):(\$2) w l ls 1 t sprintf('T > %03.0f abc',$= 1),\ "sprintf.txt" using (\$1):(\$3) w l ls 2 t sprintf('T > %3d abc',$2) unset table EOF After the script execution I get an eps file with the following strings tha= t "don't align properly": they look approximately like this T > 020 abc T > 1 abc instead of looking like this (the way I want): T > 020 abc T > 1 abc. As Hans-Bernhard mentioned I used %d specifier for printing a float, which = is of course wrong. Actually all the "floats" I provide for sprintf can be = converted into integers (so these floats are 20.0 and 1.0 in the above exam= ple). I used %d because I started trial and error method just to get what I= want.=20 P.S. In the reference it was written: "Minimum number of characters to be printed. If the value to be printed is = shorter than this number, the result is padded with blank spaces. The value= is not truncated even if the result is larger." So specifying sprintf('T > %3.0f abc', 1) I expected to see desired blanks= , but I didn't. Than I started applying "everything you can try" method. By the way sprintf('T > %3.0f abc', 1) did the same job as sprintf('T > %= 3d abc', 1). However sprintf('T > %5d abc', 1) had the desired output fo= rm T > 020 abc T > 1 abc but only for 1 digit numbers. Sorry, I did a pretty bad job writing my first post.