Re: printf problem



On Oct 29, 8:22 pm, jp...@xxxxxxxxxxxxxx (Julie A. Bolduc) wrote:
I am trying to format some numbers so single digit numbers are converted to
2 digit numbers and here is the code I am working with right now. For some
reason, the very last number, $b5, ends up as a 3 digit number instead of a
2 digit number even if I start it with a single digit number. If I add
chop($bcol); to the code, it cuts the last digit off but what if I do not
need to have that happen? I am not exactly new to perl but this has me
stumped.

print "Content-type: text/html\n\n";
$b1=11;
$b2=2;
$b3=5;
$b4=1;
$b5=12;
$bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);

#chop($bcol);
print qq($bcol);

Can anyone help?

printf() prints the string to the screen, and returns a true value (1)
if successful.
sprintf() returns the string

You are printing to the screen and storing the result (the 1) in
$bcol, and then printing $bcol. Make up your mind. Either change the
printf() to sprintf() or eliminate $bcol entirely.

Paul Lalli

.



Relevant Pages

  • Re: sprintf does not round floating point numbers
    ... In the format string, specify exact number of digits you want after the ... > I transcribed the sprintf output incorrectly. ... We are trying to determine whether we should ignore the last digit, ...
    (microsoft.public.vc.language)
  • Re: [PHP] sprintf() oddness
    ... sprintf('%.03f', 0.15250000) ... The 4th significant digit in both cases is '5' but in the first case, ... Shouldn't rounding decisions be based on subsequent digits and not preceding ... What you describe is "if even then round down else round ...
    (php.general)
  • Re: How to control space between elements in printing a matrix
    ... |> I have a doubt regarding the printing of elements in a very big ... | List directed formatting will break lines such ... Nor will most printers. ... | then three digit numbers will be printed with three spaces between ...
    (comp.lang.fortran)
  • Re: [PHP] sprintf() oddness
    ... sprintf('%.03f', 0.15250000) ... The 4th significant digit in both cases is '5' but in the first case, ... it's more accurate to use the preceding digit to determine rounding. ... What you describe is "if even then round down else round up". ...
    (php.general)
  • Re: can anyone advise me
    ... In your nested loop you are printing an x followed by zero or ... you switch to a new line. ... So the last digit on each line is an x, except for the last line, ...
    (comp.lang.python)