×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

# 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

Errmm - don't printf and sprintf just truncate the values - as opposed to rounding them?

### 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

### RE: A little math

Ok - that sounds like reasonable behaviour - I'll bear it in mind, thanks Andy.

#### 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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!