tmaly has received some excellent advice above and as I understand your contribution, it is an extension of that wisdom. For clarity, for future Seekers, however, to make its relevance explicit (and ramble a bit):

The essential point (IMO, YMMV) in most of the earlier replies is:

Don't treat the phone number as a number!

Granted, it's not clear how tmaly is getting the phone number as a number to begin with, and your sprintf("%d\n",$num); should help, if he has no control over the initial acquisition (if, for example, he's dragging the phone numbers out of a db where the DBA tried to save a few chars by omitting the usual phone number formatting, such as parens around an area code, or dashs or dots to separate the groups).

But, if he can avoid ever having the phone number treated as a number, he can avoid myriad issues:

use strict; use warnings; my $string = '99999999999999999999999999'; my $string2 = '9-999-999-999-999-999-999-999-9999'; # see Note 1 below if ( $string =~ /^(\d)(\1|-)+$/ ) { print "\$string might be a phone number: $string \n"; } else { print "$string is NOK \n"; } if ( $string2 =~ /^(\d)(\1|-)+$/ ) { print "\$string2 might be a phone number: $string2 \n"; } else { print "$string2 is NOK \n"; } print "stiller's snippet: \n"; my $num = 999999999; print sprintf("%f\n",$num); print sprintf("%d\n",$num);

Note the NON-interpolating quoting of the strings; also added alternation on "-" as this is one of the common formatting schemes mentioned above (and is used in $string2. Output looks like this:

$string might be a phone number: 99999999999999999999999999 # se +e Note 2 $string2 might be a phone number: 9-999-999-999-999-999-999-999-9999 # + also see Note 2 stiller's snippet: 999999999.000000 # a float, as stiller noted, this address's OP's pro +blem, 999999999 # whereas, Perl makes it easy to use this as a strin +g

1 Interpolating string 2 treats the string as a number and the dashes as subtraction operators
2 A 26 digit phone number? Isn't this rather longer than one might expect, even allowing for international access codes and an implicit extension? It is in North American useage, but YMMV elsewhere.


In reply to Re^2: Question on converting big numbers to strings without rounding by ww
in thread Question on converting big numbers to strings without rounding by tmaly

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.