# convert number to format

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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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;
}
Commented:
2.3 is to be rounded to 2 which is to be 00011

That should be 00010

Commented:
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);
}
Commented:
If you want the output to be as you posted, then change the return statement to this:

return sprintf ("%0.5d", 1 x \$n);
Author Commented:
thanks for to all for all the comments, arjanh had the fastest code when I benchmarked them.

thanks again
Commented:
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));
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.