Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math conversion

Status
Not open for further replies.

jray2003

Programmer
Aug 12, 2003
253
0
0
US
Is there a PM that can do the following.

Take the following

40000000000 = 40 GIGs
200000000 = 200 MB

Thanks,

Jim

 
Hi,

Are you looking at cases with only one non-zero leading digit?

I.E. Avoid cases like 220,000,000 = 220M?

Now if you are considering all numbers, for numbers like 4,551,000,000, do you expect to get 4.551G or without the decimal 4,551M?

 
There might be a module for all I know, but you can handle this easily with a private function:

there might be for all I know but that is a pretty easy conversion using a private function:

Code:
my @sizes = (40000000000, 3000000000, 200000000, 7000000, 5000, 234);

print get_size($_)."\n" for @sizes;

sub get_size {
   local $_ = shift;  
   if ($_ >= 1024000000) {
      $_ /= 1024000000;
      $_ = sprintf("%.2f", $_);
      return("$_ GIGs");
   }
   elsif ($_ >= 1024000) {
      $_ /= 1024000;
      $_ = sprintf("%.2f", $_);
      return("$_ MBs");
   }
   elsif ($_ >= 1024) {
      $_ /= 1024;
      $_ = sprintf("%.2f", $_);
      return("$_ KBs");
   }
   else {
      return("$_ BYTES")
   }
}

the code could be reduced but I wanted to show you an example that shows clearly a possible way of doing the conversions. For a single variable:

my $size = 40000000000;
$size = get_size($size);
print $size;
 
Kevin,

You're the man and thank you for all you have tought me over the months. This is what I am wanting to do. I have learned much from you and hope one day I can give back to this site what it has given me.

The Mod I am using is the Win32::AdminMisc and it has some problems with it. First, it won't display the serial number of the hard dive and I need it to do so and this code will help me with the other numbers.

Thanks again for not making me so stupid and I have gained so much from you and others here that have responed to my stupid questions. This site is better the others because of that.

Kodos!

Jim
 
Code:
[b]#!/usr/bin/perl[/b]

@sizes = (40000000000, 3000000000, 200000000, 7000000, 5000, 234);

foreach (@sizes) {
  s/^(\d+)(\d{9})$/$1.$2 GIGs/;
  s/^(\d+)(\d{6})$/$1.$2 MB/;
  s/^(\d+)(\d{3})$/$1.$2 KB/;
  s/\.0+( [A-Z]{2,3}[a-z]?)$/$1/;
  s/^(\d+)$/$1 BYTES/;
  print "$_\n";
}

yuk!


Kind Regards
Duncan
 
How about:
Code:
my @sizes = qw/B KB MB GB TB/;
my @numbers = (40000000000, 3000000000, 200000000, 7000000, 5000, 234);
my @print = map {convert($_)} @numbers;

sub convert ($) {
    my $num =  length($_[0]) % 3 == 0 ? int(length($_[0]) / 3) - 1 : int(length($_[0]) / 3);
    return sprintf "%.02f %s", $_[0] / (1024**($num)), $sizes[$num];
}
One note with using printf/sprintf for rounding; if you have, for example, .15 - you won't always get .2 as you'd expect. Read this link for more info.
 
Dang, so many good ways of doing it.. LOL. It's like the 3 Bears and trying out which bed is best....

This is GREAT stuff!
 
There are many ways to do something this but take a close look at each way, there are some significant differences that might be important.
 
oh I love the challenge. This is what separtates others sites from the others. Here, you get the answers, but more you get educated and don't get made to feel stupid. This site is worth donating to for I feel we have the best people answering the questions.

I have much to learn and have purchased a couple of books, but not sure if they were the right ones. Can you recommend some good books that would help me learn this better? I do learn better from examples and being an old dude things come slower.

Thanks!

Jim
 
I recommend you get the Perl BookShelf Reference, its several books, on CD or papaerbacks. You can get it on amazon.ocm or ebay, very cheap. It has several books, all of which are good, including two well known books:

Learning Perl
Perl Cookbook

 
Looking for it now and this is what I have found so far.

The Perl CD Bookshelf, version 2.0. Is this the correct one?

 
The Perl Cd Bookshelf: 6 Bestselling Books on Cd-Rom Version 4.0 (Software)

by O'Reilly, Inc. Associates


It's on it's way with over night delivery.

:)
 
A possible point of interest with this problem - namely, the conversions between measurements.

The OP asked about converting 200000000 Bytes to 200 MB, but that conversion isn't entirely accurate.

This might be important:
Code:
1 KB | 1024       Bytes | (1024**1)
1 MB | 1048576    Bytes | (1024**2)
1 GB | 1073741824 Bytes | (1024**3)
 
a recursive subroutine:-

Code:
[b]#!/usr/bin/perl[/b]

@units = qw( bytes KB MB GIGs );

@sizes = (40000000000, 3000000000, 200000000, 7000000, 5000, 234);

foreach (@sizes) {
  computeSize($_, $x=3);
}

sub computeSize {
  $size = shift;
  $size /= (1024 ** $x);
  if ($size >= 1) {
    printf("%.2f", $size);
    print " " . $units[$x] . "\n";
  } else {
    computeSize($_, $x--);
  }
}


Kind Regards
Duncan
 
by far the easiest way i have found so far:-

Code:
[b]#!/usr/bin/perl[/b]

@units = qw( bytes KB MB GIGs );

@sizes = (40000000000, 3000000000, 200000000, 7000000, 5000, 234);

foreach (@sizes) {
  $x = 3;
  do { $size = $_ / (1024 ** $x--) } until $size >= 1;
  print int($size*100)/100 . "\t" . $units[$x+1] . "\n";
}


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top