Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Answer: How do I round a number?

( #161255=categorized answer: print w/ replies, xml ) Need Help??

Q&A > math > How do I round a number? contributed by powerman

Here shown all round-like functions which exists in perl:
#!/usr/bin/perl use POSIX; @a=(3.3, 3.5, 3.7, -3.3, -3.5, -3.7, 3.45); print "number\tint\tprintf\tfloor\tceil\n"; printf "%.2f\t%.1f\t%.1f\t%.2f\t%.2f\n", $_, int, $_, floor($_), ceil($_) foreach (@a);
This code produce this output:
number	int	printf	floor	ceil
3.30	3.0	3.3	3.00	4.00
3.50	3.0	3.5	3.00	4.00
3.70	3.0	3.7	3.00	4.00
-3.30	-3.0	-3.3	-4.00	-3.00
-3.50	-3.0	-3.5	-4.00	-3.00
-3.70	-3.0	-3.7	-4.00	-3.00
3.45	3.0	3.5	3.00	4.00

Comment on Answer: How do I round a number?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-07-04 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls