char to number conversion w/ a twist 
Author Message
 char to number conversion w/ a twist

Can somebody point me in the right directon or help?  I need to store
the following example chars in number format.  However, each character
will have a n*.n[n][n][KMB] type format

Easier to explain what the input will be and what I would like to get
back:

Input/Output
24.0K/24000
24.2K/24200
24.21K/24210

3.1M/3100000
3.10M/3100000
3M/3000000
3.31M/3310000

2.245B/2245000000
2B/2000000000
2.0B/2000000000

etc.

Any ideas?

Thanks,
Michael



Wed, 23 Nov 2005 19:17:27 GMT  
 char to number conversion w/ a twist
%mult = (
K=>1000,
M=>1000000,
B=>1000000000
);

$in =~ /([0-9\.]+)([KMB])/;

$out = $1 * $mult{$2};

Quote:

> Can somebody point me in the right directon or help?  I need to store
> the following example chars in number format.  However, each character
> will have a n*.n[n][n][KMB] type format

> Easier to explain what the input will be and what I would like to get
> back:

> Input/Output
> 24.0K/24000
> 24.2K/24200
> 24.21K/24210

> 3.1M/3100000
> 3.10M/3100000
> 3M/3000000
> 3.31M/3310000

> 2.245B/2245000000
> 2B/2000000000
> 2.0B/2000000000

> etc.

> Any ideas?

> Thanks,
> Michael

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Wed, 23 Nov 2005 19:45:29 GMT  
 char to number conversion w/ a twist

Quote:

> Can somebody point me in the right directon or help?  I need to store
> the following example chars in number format.  However, each character
> will have a n*.n[n][n][KMB] type format

> Easier to explain what the input will be and what I would like to get
> back:

> Input/Output
> 24.0K/24000
> 24.2K/24200
> 24.21K/24210

> 3.1M/3100000
> 3.10M/3100000
> 3M/3000000
> 3.31M/3310000

> 2.245B/2245000000
> 2B/2000000000
> 2.0B/2000000000

> etc.

> Any ideas?

> Thanks,
> Michael

one way:

my %mult = (
            K => 1000,
            M => 1000000,
            B => 1000000000,
           );


            24.0K
            24.2K
            24.21K
            3.1M
            3.10M
            3M
            3.31M
            2.245B
            2B
            2.0B
           /;


   print "$_: ";
   unless (/^(.+?)([KMB])$/) {
      print "can't parse!";
   }
   else {
      print ($1 * $mult{$2});
   }
   print "\n";

Quote:
}

yields:

24.0K: 24000
24.2K: 24200
24.21K: 24210
3.1M: 3100000
3.10M: 3100000
3M: 3000000
3.31M: 3310000
2.245B: 2245000000
2B: 2000000000
2.0B: 2000000000

hth-

--
Michael Budash



Wed, 23 Nov 2005 19:55:22 GMT  
 char to number conversion w/ a twist

Quote:

> Can somebody point me in the right directon or help?  I need to store
> the following example chars in number format.  However, each character
> will have a n*.n[n][n][KMB] type format
...
> Michael

   s/(.*)([KMB])/$1*{K=>1e3,M=>1e6,B=>1e9}->{$2}/ei;

--
Bob Walton



Wed, 23 Nov 2005 21:40:21 GMT  
 char to number conversion w/ a twist

Quote:



> > Can somebody point me in the right directon or help?  I need to store
> > the following example chars in number format.  However, each character
> > will have a n*.n[n][n][KMB] type format

> > Easier to explain what the input will be and what I would like to get
> > back:

> > Input/Output
> > 24.0K/24000
> > 24.2K/24200
> > 24.21K/24210

> > 3.1M/3100000
> > 3.10M/3100000
> > 3M/3000000
> > 3.31M/3310000

> > 2.245B/2245000000
> > 2B/2000000000
> > 2.0B/2000000000

> > etc.

> > Any ideas?

> > Thanks,
> > Michael

> one way:

> my %mult = (
>             K => 1000,
>             M => 1000000,
>             B => 1000000000,
>            );


>             24.0K
>             24.2K
>             24.21K
>             3.1M
>             3.10M
>             3M
>             3.31M
>             2.245B
>             2B
>             2.0B
>            /;


>    print "$_: ";
>    unless (/^(.+?)([KMB])$/) {
>       print "can't parse!";
>    }
>    else {
>       print ($1 * $mult{$2});
>    }
>  print "\n";
> }

> yields:

> 24.0K: 24000
> 24.2K: 24200
> 24.21K: 24210
> 3.1M: 3100000
> 3.10M: 3100000
> 3M: 3000000
> 3.31M: 3310000
> 2.245B: 2245000000
> 2B: 2000000000
> 2.0B: 2000000000

> hth-

Excellent work Superfriends!

new to perl, this stuff is so powerful, yet so confusing.  Thanks for
giving me a boost - problem sovled!

Thanks to all



Wed, 23 Nov 2005 22:47:11 GMT  
 char to number conversion w/ a twist
Spoke to soon! ;)

I can do an indivual char/number conversion with the code previously
provided, but how can I use it to manipulate the following data?

Input:
32,18,27,172,483.0M,,6.23,,1,3,20.0K,116.0K

Desired Output:
32,18,27,172,483000000,,6.23,,1,3,20000,116000

Im trying to figure something out at the moment...



Wed, 23 Nov 2005 22:58:09 GMT  
 char to number conversion w/ a twist

Quote:

> Input:
> 32,18,27,172,483.0M,,6.23,,1,3,20.0K,116.0K

> Desired Output:
> 32,18,27,172,483000000,,6.23,,1,3,20000,116000

-------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $in = '32,18,27,172,483.0M,,6.23,,1,3,20.0K,116.0K';
print "$in\n";

$in =~ s{ ([^,]+)  ([MK])  (?=,|$) }
        {
           $2 eq 'K'   ?   $1 * 1_000   :   $1 * 1_000_000;
        }xge;
print "$in\n";
-------------------------

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 24 Nov 2005 02:34:30 GMT  
 char to number conversion w/ a twist

Quote:

> Spoke to soon! ;)

> I can do an indivual char/number conversion with the code previously
> provided, but how can I use it to manipulate the following data?

> Input:
> 32,18,27,172,483.0M,,6.23,,1,3,20.0K,116.0K

> Desired Output:
> 32,18,27,172,483000000,,6.23,,1,3,20000,116000

> Im trying to figure something out at the moment...

Just for fun try:
$in = "32,18,27,172,483.0M,,6.23,,1,3,20.0K,116.0K";
print "$in\n";
$in=~ s/(\d+\.*\d+)([MBK])/$1*{K=>1e3,M=>1e6,B=>11e9}->{$2}/eig;
print "$in\n";

Thanks to Bob, but I could not get yoiur answer from his code.
Enjoy



Fri, 25 Nov 2005 12:53:43 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Because twisted minds think twisted things...

2. Random Numbers with a Twist

3. char to hex conversion

4. conversion decimal->hex number

5. Roman number conversion

6. currency number to text conversion

7. Conversion of month number to month name

8. Perl number conversion Utilities

9. Counting number of chars in a line (part of reading multiline CSV

10. Binary and Hex number conversion

11. More advanced number conversion.

12. number (6 -> 06) conversion question

 

 
Powered by phpBB® Forum Software