Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

convert last digit in decimal

by baperl (Sexton)
on Aug 10, 2011 at 20:30 UTC ( [id://919735]=perlquestion: print w/replies, xml ) Need Help??

baperl has asked for the wisdom of the Perl Monks concerning the following question:

hi, My code allows me to download the mkt cap from Yahoo. however, the mkt cap shows up as 28.04B or 376.92M. when I dump the values into the db, they go in as 28 or 377, which is obviously not correct. is there a way I can look up the mkt cap and if it ends with a B, multiply by a billion and if M, multiply by a million, and then insert the correct values into the db. Could you please help me figure it out? thanks!

Replies are listed 'Best First'.
Re: convert last digit in decimal
by AnomalousMonk (Archbishop) on Aug 10, 2011 at 21:38 UTC

    This is a bit over-generalized, but allows for easy extension.

    >perl -wMstrict -le "use Regexp::Common qw(number); ;; my %conversion = ( K => 1_000, M => 1_000_000, B => 1_000_000_000, L => 100_000, LAKH => 100_000, ); my $suffix = qr{ (?i) @{[ join q{|}, reverse sort keys %conversion ]} }xms; ;; for my $n (qw(28.04B 376.92M 12.3K 98.76Lakh 7l 23.45k), @ARGV) { my ($base, $mult) = $n =~ m{ \A ($RE{num}{real}) ($suffix) \z }xmsg; print qq{'$n' => }, $base * $conversion{uc $mult}; } ;; my $s = 'w 28.04b x 376.92M y 98.76L z'; $s =~ s{ ($RE{num}{real}) ($suffix) } { $1 * $conversion{uc $2} }xmsge; print qq{'$s'}; " '28.04B' => 28040000000 '376.92M' => 376920000 '12.3K' => 12300 '98.76Lakh' => 9876000 '7l' => 700000 '23.45k' => 23450 'w 28040000000 x 376920000 y 9876000 z'

    Update: Added multiple conversions in a string to example.

Re: convert last digit in decimal
by ikegami (Patriarch) on Aug 10, 2011 at 21:40 UTC
    my %mul = ( M => 1e6, B => 1e9, ); if ($num =~ s/([^0-9.]+)\z//) { my $suf = $1; die if !$mul{$suf}; $num *= $mul{$suf}; }

    (Billion can also mean 1e12. Adjust if necessary.)

      thanks ikegami, this isn't working though.... here's what I have
      my $mult=chop($mcap); my $dec=$mcap-int($mcap); if ($mcap =~ s/([^0-9.]+)\z//) { my $suf=1; die if !$multiplier{$suf}; $mcap *= $multiplier{$suf}; } print "multiplier is $mult and decimal portion is $dec and mcap is $mc +ap\n";
      and this is the output
      multiplier is B and decimal portion is 0.200000000000003 and mcap is 1 +08.2
        Then I recommend you use the code I posted.
Re: convert last digit in decimal
by AR (Friar) on Aug 10, 2011 at 20:43 UTC

    What have you tried?

    You could separate the decimal from the multiplier using a regular expression, use a hash (that maps "M" to 1e6 and "B" to 1e9), multiply the decimal by the multiplier and finally insert into the database.

      thanks for your suggestion AR, I will try it. I was trying to get the length, and then determine the last character, but that was not working out.

        You could also chop and save the last character if you know that the format is always going to be the same. That's a bit simpler than a regex, but it presumes you know the format is constant.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found