CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Quick question on format specifiers

 



yoplocheo
Novice

Dec 21, 2015, 11:33 PM

Post #1 of 2 (1026 views)
Quick question on format specifiers Can't Post

Hi peeps!
Was coding such a line:

Code
printf "The formatted number is (%10d)\n", 100;

I understand that the specifier %d is for decimal numbers, whereas %s is for strings. Just wondering, what are the implications of displaying "100" as a string instead of a variable, since in this case the output when using either specifiers are the same?


Laurent_R
Veteran / Moderator

Dec 22, 2015, 1:31 PM

Post #2 of 2 (999 views)
Re: [yoplocheo] Quick question on format specifiers [In reply to] Can't Post

In that specific case, it does not change anything and both formats should just print "( 100)".

But consider this:

Code
printf "The formatted number is (%10d)\n", 100.25;

It will convert 100.25 into an integer and print "( 100)".

With a string format:

Code
printf "The formatted number is (%10s)\n", 100.25;

it will print 100.25 as a string: "( 100.25)"

And this floating point formatting :

Code
 printf "The formatted number is (%10.4f)\n", 100;

will print: "( 100.0000)"

Does that make these formats clearer?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives