If I correctly understand what is happening here, the letter 'D' is a locale-specific exponentiation letter: what locale are you using? Since the output shows that perl is parsing the number correctly, I'd guess that the "Argument isn't numeric" warning is a bug in perl - that it is checking only for "E" as the exponentiation letter - but that the operating system library is then correctly converting the number.

If that is correct, the best workaround I can think of is to locally disable the warning, and check the correctness yourself:

sub weird2number { my $text = shift; unless ($text =~ /^\d*\.\d+D[+\-]\d+$/) { warn qq{Argument "$text" is not weird}; } no warnings "numeric"; return $text + 0; }

If you also need to output numbers in this format, you may also have problems: the closest is the (s)printf '%E' format, which puts the first significant digit before the decimal point. The simplest way I can think of to get around that is to use sprintf '%E', $number and then modify the result, perhaps like so:

sub number2weird { my $number = shift; my $text = sprintf '%.14E', $number; $text =~ s{ (\d) \. (\d+ [DE]) ([+\-] \d+) }{ "0.$1$2" . sprintf "%+03d", $3 + 1 }xe or warn qq{Failed to make "$text" weird}; return $text; }

Note however that the output format of sprintf '%E' is also dependent on your operating system libraries, so you may need to modify the pattern to match correctly.

Hugo

In reply to Re: Weird number formatting by hv
in thread Weird number formatting by licking9Volts

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.