Hi all, this is my first ever post at PerlMonks.

Where I work, we need to know that "240n" humanly equates to "0.24u" == .24e-6 == 240 * 1e-9

Numerifying an input string (usually human-entered) allows for practical numerical operations/comparisons. Obviously, I made some assumptions on how a number is to be represented. For instance, "." alone is an invalid number and does not mean "0.0".

Suggestions for optimizations, a better way of thinking/writing it, using modules instead, etc. are appreciated.
#!/usr/bin/perl -wl use strict; ####################### ## string numerifier ## ####################### # SI prefix to exponent value conversion table my %SI = ( Y => +24, # yotta Z => +21, # zetta E => +18, # exa P => +15, # peta T => +12, # terra G => +9, # giga M => +6, # mega k => +3, # kilo K => +3, # kilo "" => 0, # default is no SI prefix m => -3, # milli u => -6, # micro n => -9, # nano p => -12, # pico f => -15, # femto a => -18, # atto z => -21, # zepto y => -24, # yocto ); sub num { my ($numstr) = @_; # string must contain digit if ($numstr =~ /\d/) { # clean string for best chance at numerification my ($sign, $exponent, $siprefix) = ("+", 0, ""); if ($numstr =~ /^[^.]*\d/) # does first digit appear before po +tential decimal point? { $numstr =~ s/^.*?([+-]?)(\d+[.]?\d*)(?:[eE]([+-]?\d+))?([Y +ZEPTGMkKmunpfazy])?.*/$2/; $sign = $1 if $1; $exponent = $3 if $3; $siprefix = $4 if $4; } else { $numstr =~ s/^.*?([+-]?)(\d*[.]?\d+)(?:[eE]([+-]?\d+))?([Y +ZEPTGMkKmunpfazy])?.*/$2/; $sign = $1 if $1; $exponent = $3 if $3; $siprefix = $4 if $4; } # convert SI prefixes -- example: ".16E2m" => ".16e-1" => 0.01 +6 if ($exponent || $siprefix) { $exponent += $SI{$siprefix}; $numstr .= "e".$exponent; } # numerify string according to nearest sign { no warnings 'numeric'; $numstr = -+-$numstr; # high-precedence nu +merifier $numstr = -$numstr if $sign eq "-"; # negative if last s +ign is "-" } } else { $numstr = undef; } return($numstr); } ############## # test cases # ############## print "|".num("+-- 123.mA")."|"; # output: "|0.123|" print "|".num(" -+--.15e4u99..99.0")."|"; # output: "|-0.0015|" print "|".num(" -+-- 789. G5e2")."|"; # output: "|789|" print "|".(1e-6 == num("1e-6"))."|"; # output: "|1|" print "|".(1e-6 == num("1e3n"))."|"; # output: "|1|" print "|".(1e-6)."|".(1e3*1e-9)."|"; # output: "|1e-06|1e-06|" print "|".(1e-6 == 1e3*1e-9)."|"; # output: "||" -- low-level FP l +imitation, perhaps with C's strtod()

In reply to String numerifier with SI prefix support by repellent

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.