Solved

perl question

Posted on 1998-06-20
5
147 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
Comment Utility
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
Comment Utility
push(@array,$1,$2,$2&&sprintf("%.2f",eval($formula))) if /^(\S+)\s+(\S+)/;

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
# 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
Comment Utility
i think the second comment made by ozo is perfect.
0
 
LVL 4

Expert Comment

by:martinag
Comment Utility
Didn't you want ozo's answer?
If there's been a misunderstanding here, just let us know.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

12 Experts available now in Live!

Get 1:1 Help Now