×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

convert a string to number and divide
2

convert a string to number and divide

convert a string to number and divide

(OP)
I know this is basic a stupid, am sorry.

I am retrieving a value from a table and I need to divide it by another value that I have retrieved.

The current code does not work.

CODE


# read POVAGRMTLN table
        my $stmt = "SELECT POVAGRMTLN.BASE_COST
           FROM LAWSON.POVAGRMTLN
           WHERE POVAGRMTLN.PROCURE_GROUP  = '" . $vVendGr . "' AND
                 POVAGRMTLN.ITEM           = '" . $vItem . "' AND
                 POVAGRMTLN.HOLD_FLAG      = '" . $vFlag . "'";

        my @rev = $dbh->selectrow_array($stmt);
        $field[23] = printf "%10.4f"($rev[0] / $vConv);

        print OU2 join('|', $vItem, $vConv, $field[23], "\n");

I am trying to define $rev[0] and $vConv to both be of a length of 10 and a decimal precission of 4. With that result going into $field[23]. Do I need to define each separately then do the math?

RE: convert a string to number and divide

I tried to divide 2 strings which contain numbers, but it seems to work without problem.
For example, this code

CODE

use strict;
use warnings;

my $str01 = '3.14159';
my $str02 = '2.0';
my $res = $str01/$str02;

print "\$str01 = $str01, \$str02 = $str02, \$res=\$str01/\$str02 = $res\n";
gives this output:

CODE

c:\Work>perl str2num.pl
$str01 = 3.14159, $str02 = 2.0, $res=$str01/$str02 = 1.570795

RE: convert a string to number and divide

OK, it does not work, but what is it giving?
The  printf  is incorrect, there should be a comma after the format string.

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: convert a string to number and divide

I'm going to assume that all the lines leading up to the 'printf' line are working. For the calculation you're trying to do, you want to use sprintf. Something like this will probably work for you:

CODE

$field[23] = sprintf "%10.4f", ($rev[0] / $vConv);

Now that is going to lead to a problem with half-way-point alternation (take a look at perlfaq4 for an explanation.)

In the past, I have used a function similar to the one in the following code to do rounding. I'm sure it could be made to run faster but it's worked for me.

CODE

foreach (@arr) {
    my $temp = sprintf '%10.4f', myround($_, 4);
    print $temp, "\n";
}

sub myround {
    my ($float, $precision) = @_;
    my $capture = $precision + 1; #number of decimal places to capture
    if (m/([^.]+)(\.\d{$capture})/) {
        my ($ant, $cons) = ($1, $2);
        $cons = int($cons * 10**($precision)+0.5) * 10**(-$precision);
        $cons = $cons * -1 if $ant < 0;
        return $ant + $cons;
    } else {
        return $float;
    }
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close