×
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!

*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

A little math

A little math

A little math

(OP)
Let's say I have 3.567653

I need to print this out round to the nearest 10th.

In otherwords 3.6

Thanks!

RE: A little math

If you know that your result will always be:

X.Y

i.e. one digit before and another after the decimal point...

use:
print "%1.1f", $num;

or if you don't know how many digits before the point you want:


$num = 3.5675737;

($before,$after) = split(/\./,$num);
$before = length $before;

printf "%$before.1f" , $num;


Hope that helps
Loon

RE: A little math

(OP)
Ok, but I have to add together the rounded answers.  I need to print 3.6 but also need to add 3.6 to something else.

Is there something to round it and still be able to add them together?  

Will what you gave me be able to do that?

RE: A little math

The printf doesn't change the value of the scalar no.. it's still 5.56737 etc..

I'm sure there'll be a way to do it - unfortunately I have no idea how, I suspect there's a CPAN module for this kind of thing somewhere.

Failing that, could you do the arithmetic first and then round out for the printing at the end?

Good luck!
Loon

RE: A little math

There's a companion to the printf function that you can use to format a value, and assign that value to another scalar: sprintf.  Here's a quick example:

$Value = 3.141579;    # Whatever ;^)

$Rounded = sprintf("%1.1f", $Value);

print "Original: ", $Value, "\n";
print "Rounded:  ", $Rounded, "\n";




--
0 1 - Just my two bits

RE: A little math

They round, but slightly differently from what you may be used to.  (At least in the quick test I just did :)

For example:  0.01 to 0.50 will display as 0.

0.51 to 0.99 will display as 1.

0.123 with a format of %1.1f will display as 0.1, as will 0.126.  (This gives you 0.13 which in turn rounds down to 0.1.)

I was always taught that 0.5 and about should round up, but perl seems to do it as anything over 0.5 will round up...




--
0 1 - Just my two bits

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