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!

how do I convert a number in string format to a number?

Status
Not open for further replies.

nexus1001

IS-IT--Management
Sep 27, 2001
13
0
0
DE
Hey does anyone happen to have a way to convert a string to int format?

$var = '2' being different from $var = 2.

This is causing me trouble...any help appreciated guys
 
no such thing in Perl.
One of Perl's distinctions (good or bad depending on your perspective) is that vars are not typed. This can be frustrating when trying to maintain a certain level of precision with your values or a certain reporting format. There are a few modules that can help...... ' might check CPAN.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks man, I'll have a look...bit diapointed though...I've run into this problem a few times now.

(typical example is trying to generaste a random number from a numerical argument passed through a function)

here's the code snippet..if you ve got something simpler let me know (but this does not wark anyway..it would have if the arg were truly numeric and not in string format...)

(in this example I tried to do it through hash values but they are converted to string too...grmpf!!)

if($fld_type eq 'NB')
{
my $nb = shift if defined($_);
foreach (0..($numeric{$nb}-1))
{
$fld .=(int(rand($numeric{$nb})));
 
A good way to convert a string to an int is to coerce it using:
Code:
$a = '00002.3';  print "$a\n";
{
    use integer;
    $a -= 0;     print "$a\n";
}
Cheers, Neil :)

 
Nexus1001,

What GoBoating is saying is that Perl is very context sensitive and tried very hard to do what you want, which may not be what you meant.

Consider, for example:

Code:
#!/usr/bin/perl

$var_A = '3.1415927 is a rough approximation of pi.';
$var_B = '2 units is the radius of this circle.';
$diameter = ( $var_A ) * ( ( $var_B ) ** 2 );

print "Diameter = $diameter\n",
      "Var_A = $var_A\n",
      "Var_B = $var_B\n",
      "Funny how Perl works, eh?\n";

If you run this, you'll get an appropriate diameter (if I'm remembering my basic geometry correctly).

It's also interesting to note that Perl knows when you're doing stuff like this. Try this variation:

Code:
#!/usr/bin/perl -w
use strict;

my $var_A = '3.1415927 is a rough approximation of pi.';
my $var_B = '2 units is the radius of this circle.';

my $diameter = ( $var_A ) * ( ( $var_B ) ** 2 );

print "Diameter = $diameter\n",
      "Var_A = $var_A\n",
      "Var_B = $var_B\n",
      "Funny how Perl works, eh?\n";

It still works, but you'll notice that you get a couple of warnings messages from Perl.

For a more complete treatment of the issue, please see
Hope this helps...

-- Lance
 
Sorry, and surely you can just use the int subroutine?
Something like the following:
Code:
sub rands {
    my $num = int( shift );
    my @arr = ();
    for( 0 .. $num ) {
        push @arr, int( rand( $num ) );
    }
    return \@arr;
}

$" = "\n";
print "random numbers:\n@{ rands( '20.3' ) }\n";
Cheers, Neil
 
Like Neil states you should be able to use the int() routine to make absolutely sure you are dealing with an integer. I general use it ignore points, but if you're having curious errors with number strings, int() should sort you out.

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top