Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

String numerifier with SI prefix support

by repellent (Priest)
on Jan 31, 2008 at 23:46 UTC ( [id://665487]=CUFP: print w/replies, xml ) Need Help??

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()

Replies are listed 'Best First'.
Re: String numerifier with SI prefix support
by toolic (Bishop) on Feb 01, 2008 at 01:18 UTC
    Interesting. I have a script which perfoms essentially the inverse operation. Given a floating-point numerical input, I produce an output string formatted in engineering notation. For example:
    0.000456 --> 456u
    My script has fewer bells and whistles, and I hadn't even considered the [yYzZ] ranges :)
      Hello from JAHW engineer :) So,

          yourfunc(0.00000456) => "4.56u" or "456e-2u" ?
        My function "autoscales", so it would output "4.56u". But, I could see the value in having a mode where it could fix the scale to always output "u", or whatever the user specifies. Here is a sample of what my function does (left column is input, right is output):
        1234 --> 1.23k 50000 --> 50k 5000 --> 5k 500 --> 500 0.012 --> 12m 0.00055555 --> 555u 5e-5 --> 50u 5E-5 --> 50u 5.0e-05 --> 50u -193.7 --> -193 -5 --> -5 -0.7895 --> -789m 555555555555555555 --> 5.56e+17 0.000000000000000777 --> 7.77e-16 9876543 --> 9.87M

        Obviously, this is only used for very coarse estimates. It does not handle rounding or precision correctly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://665487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found