Solved

perl question

Posted on 1998-06-20
5
159 Views
Last Modified: 2010-03-04
I wrote following code. But the problem is suppose $1=10, $2=3.the $value will be 3.33333. But how can I only keep 2
characters after decimal point.That is, I want the value of $value pushed into @array is 3.33.

    ----------------------------------------
    $formula='$1/$2';

    while(<>){
    /^(/S+)/s+(/S+)/
      if($2 == 0){
         $value=0; #is there any concise way to do this?
      }else{
         $value=eval($formula);}
      push(@array,$1,$2,$value,"\n");
    }

    ............




0
Comment
Question by:rongz
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
martinag earned 100 total points
ID: 1207935
Multiply with 100, round and divide by 100.

$value *= 100;
$value = int($value);
$value /= 100;

For example, 3.3333 is multiplied with 100.
That gives us 333.33
Then we round it: 333
When we divide it by 100 we get 3.33
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207936
push(@array,$1,$2,$2&&sprintf("%.2f",eval($formula))) if /^(\S+)\s+(\S+)/;

0
 
LVL 84

Expert Comment

by:ozo
ID: 1207937
# or just in case $2 == 0 but not $2 eq '0':
  push @array,$1,$2,0+$2&&sprintf "%.2f",eval $formula
0
 

Author Comment

by:rongz
ID: 1207938
i think the second comment made by ozo is perfect.
0
 
LVL 4

Expert Comment

by:martinag
ID: 1207939
Didn't you want ozo's answer?
If there's been a misunderstanding here, just let us know.
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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

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

13 Experts available now in Live!

Get 1:1 Help Now