Solved

convert number to format

Posted on 2003-12-09
7
425 Views
Last Modified: 2010-03-04
Hello

I need to convert numbers like .7 and 2.3 to 00001 and 00011 respectivly.

examples
0.7 is to be rounded to 1 which is to be 00001
2.3 is to be rounded to 2 which is to be 00011
4.5 is to be rounded to 5 which is to be 11111

so here is my best shut
my $results = flt2bit5 ( .7);

sub flt2bit5 {
    my $n = shift;
    my $n = sprintf ("%.0f", $n);
   ´
I am not sure ....

thanks
0
Comment
Question by:samj
7 Comments
 
LVL 5

Accepted Solution

by:
arjanh earned 50 total points
ID: 9906803
This does the trick:

sub flt2bit5 {
   my $n = shift;
   my $res;
   for( my $i=5-int($n+0.5); $i>0; $i-- ) {
      $res .= "0";
   }
   for( $i=0; $i<int($n+0.5); $i++ ) {
      $res .= "1";
   }
   return $res;
}

int() is basically a floor() function, so by adding 0.5 you get an actual round(). You have to fill with 0 for 5-n digits and then print n times 1.
0
 
LVL 3

Expert Comment

by:terageek
ID: 9907259
You could also use the string mulitplication operator..

sub flt2bit5 {
   my ($n) = @_;
   $n = int ($n + 0.5);
   $n = 5 if ($n > 5);  # Just in case
   $n = 0 if ($n < 0);  # Just in case
   return "0" x (5 - $n) . "1" x $n;
}
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9907305
2.3 is to be rounded to 2 which is to be 00011
                                                           


That should be 00010


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:FishMonger
ID: 9907550
If you're tring to convert to binary, then this will work.

use strict;

my @num = qw(0.7 2.3 4.5);

foreach (@num) {
   my $results = flt2bit5($_);
   print "$_ is to be rounded to ".sprintf ("%.0f", $_)." which is to be $results\n";
}

sub flt2bit5 {
   my $n = sprintf ("%.0f", shift);
   return sprintf ("%.5b", $n);
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9907602
If you want the output to be as you posted, then change the return statement to this:

return sprintf ("%0.5d", 1 x $n);
0
 

Author Comment

by:samj
ID: 9908838
thanks for to all for all the comments, arjanh had the fastest code when I benchmarked them.

thanks again
0
 
LVL 3

Expert Comment

by:terageek
ID: 9908859
FishMonger has it.  You won't even need the function call, just change it to:

my $results = spirntf("%0.5d", "1" x ($input + 0.5));

If inputut is less than 0, you will still get "00000", but if $input is > 5, you will get a field more than 5 characters.  Even the error checking can be done in one line...

my $results = sprintf("%0.5d", "1" x (($input > 4.5) ? 5 : $input + 0.5));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 69
problem with using the glob function 1 63
Insert Text into odbc.ini file 15 64
Is it true to say that Python is successor of Perl? 7 138
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now