Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 imbibing at the Monastery: (15)
As of 2014-12-17 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (34 votes), past polls