# How to a print a real with before and after digits?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi All,

I am trying to figure out how to print a real number
with before and after the decimal point digits

<code>
#!/usr/bin/perl
my \$pi = 3.141592654;
print "PI = %2.3f\$pi\n";
</code>

\$ ./PrintReal.pl
PI = %2.3f3.141592654

Should be:
03.141

What am I doing wrong?

Many thanks,
-T

## Re: How to a print a real with before and after digits?

On 7/17/2015 6:54 PM, T wrote:

http://perldoc.perl.org/functions/sprintf.html

## Re: How to a print a real with before and after digits?

On 07/17/2015 06:18 PM, Scott Bryce wrote:

I actually have that howto copied into a keeper.  This
is were I got "%2.3f", which does not work.  :'(

## Re: How to a print a real with before and after digits?

On 07/17/2015 06:18 PM, Scott Bryce wrote:

Over here I am finding this example:
http://docstore.mik.ua/orelly/perl3/lperl/ch06_05.htm

"The %f conversion (floating-point) rounds off its
output as needed, and even lets you request a certain
number of digits after the decimal point:

printf "%12f\n", 6 * 7 + 2/3;    # looks like ‘‘‘42.666667
printf "%12.3f\n", 6 * 7 + 2/3;  # looks like ‘‘‘‘‘‘42.667
printf "%12.0f\n", 6 * 7 + 2/3;  # looks like ‘‘‘‘‘‘‘‘‘‘43

<code>
#!/usr/bin/perl
my \$pi = 3.141592654;
print    "PI = %.3f\$pi\n";
printf ( "PI = %.3f\n", \$pi );
printf ( "PI = %2.3f\n", \$pi );
</code>

\$ ./PrintReal.pl
PI = %.3f3.141592654
PI = 3.142
PI = 3.142

The "2" or greater does not add forward digits when
used with the "printf" command and does not work
at all with the "print" command.

So far, the onoy thing I can et working is the number
after the decimal point and only with the "printf"
command.

## Re: How to a print a real with before and after digits?

On 07/17/2015 06:57 PM, T wrote:

Just figured some of it out.

The number after the decimal point is the number of digits
past the decimal point

The number before the decimal point is the total number of digits,
INCLUDING the decimal point.

\$ perl -e 'printf ( "Monthly interest rate: <%4.2f%%>\n", 5.25/12 )'
Monthly interest rate: <0.44%>

\$ perl -e 'printf ( "Monthly interest rate: <%5.2f%%>\n", 5.25/12 )'
Monthly interest rate: < 0.44%>

\$ perl -e 'printf ( "Monthly interest rate: <%6.2f%%>\n", 5.25/12 )'
Monthly interest rate: <  0.44%>

Now to figure out how to fill in the preceding blank space with
zeros.

And "print" still does not work.  :'(

Printf (works):
\$ perl -e 'my \$R=5.25/12; printf ( "<%12.6f%%>\n", \$R )'
<    0.437500%>

Print (doesn't work):
\$ perl -e 'my \$R=5.25/12; print "<%12.6f\$R%%>\n"'
<%12.6f0.4375%%>

## Re: How to a print a real with before and after digits?

On 7/17/2015 8:19 PM, T wrote:

And it won't, since it does not recognize the formatting string the way
that printf (print formatted) does.

## Re: How to a print a real with before and after digits?

On 07/17/2015 07:25 PM, Scott Bryce wrote:

Hi Scott,

Ah Ha!  I was starting to suspect that.  But being a novice
at Perl, I always suspect myself first.

Thank you for helping me with this.

-T

## Re: How to a print a real with before and after digits?

On 07/17/2015 06:57 PM, T wrote:

Just figured out how to get leading zeros.  Place a zero
before the total digits number.  For example (%015.6f):

\$ perl -e 'printf ( "Monthly interest rate: <%015.6f%%>\n", 5.25/12 )'
Monthly interest rate: <00000000.437500%

Still can't get this to work with plain old "print"

## Re: How to a print a real with before and after digits?

when you need special formatting you will use printf

## Re: How to a print a real with before and after digits?

On 07/18/2015 03:06 AM, George Mpouras wrote:

Finally sunk in.  I kept thinking I was at fault