Both the advice from SankoR and, even more so, the observation by FunkyMonk are clearly on target. In the hope of offering some additional clarity:

printf produces output; sprintf merely formats

However, I'm perplexed by your use of "string" in the phrase "save it as a string" as well as by some other aspects of the question. For example, do you mean that $n and $u are floating point numbers (assumed below) or can they be ints; a mixture; or something else??

All the above leads me to send a question back: Does this come somewhere close to your intent?

#!/usr/bin/perl use strict; use warnings; # 816487 my $n = 3.123; my $u = 3.678; my $i= 0; my (@array, $array); for (1..10) { $n -= ($n * $u) * $u; $array[$i] = int(abs($n)); $i++ } for (@array) { print "$_ \n"; }

Output, for $n=3.123, $u=3.678

39 490 6140 76922 963664 12072486 151240294 1894690621 23736085384 297358177092

and for $n=3.123, $u=4.0

46 702 10540 158101 2371528 35572921 533593828 8003907421 120058611328 1800879169921

And another question: "Waaaay off? Helpful?" (I hope the latter.)


In reply to Re: printf function alternative? formatting without printing by ww
in thread printf function alternative? formatting without printing by fraizerangus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.