1Round(3) User Contributed Perl Documentation Round(3)
2
3
4
6 Math::Round - Perl extension for rounding numbers
7
9 use Math::Round qw(...those desired... or :all);
10
11 $rounded = round($scalar);
12 @rounded = round(LIST...);
13 $rounded = nearest($target, $scalar);
14 @rounded = nearest($target, LIST...);
15
16 # and other functions as described below
17
19 Math::Round supplies functions that will round numbers in different
20 ways. The functions round and nearest are exported by default; others
21 are available as described below. "use ... qw(:all)" exports all
22 functions.
23
25 round LIST
26 Rounds the number(s) to the nearest integer. In scalar context,
27 returns a single value; in list context, returns a list of values.
28 Numbers that are halfway between two integers are rounded "to
29 infinity"; i.e., positive values are rounded up (e.g., 2.5 becomes 3)
30 and negative values down (e.g., -2.5 becomes -3).
31
32 round_even LIST
33 Rounds the number(s) to the nearest integer. In scalar context,
34 returns a single value; in list context, returns a list of values.
35 Numbers that are halfway between two integers are rounded to the
36 nearest even number; e.g., 2.5 becomes 2, 3.5 becomes 4, and -2.5
37 becomes -2.
38
39 round_odd LIST
40 Rounds the number(s) to the nearest integer. In scalar context,
41 returns a single value; in list context, returns a list of values.
42 Numbers that are halfway between two integers are rounded to the
43 nearest odd number; e.g., 3.5 becomes 3, 4.5 becomes 5, and -3.5
44 becomes -3.
45
46 round_rand LIST
47 Rounds the number(s) to the nearest integer. In scalar context,
48 returns a single value; in list context, returns a list of values.
49 Numbers that are halfway between two integers are rounded up or down
50 in a random fashion. For example, in a large number of trials, 2.5
51 will become 2 half the time and 3 half the time.
52
53 nearest TARGET, LIST
54 Rounds the number(s) to the nearest multiple of the target value.
55 TARGET must be positive. In scalar context, returns a single value;
56 in list context, returns a list of values. Numbers that are halfway
57 between two multiples of the target will be rounded to infinity. For
58 example:
59
60 nearest(10, 44) yields 40
61 nearest(10, 46) 50
62 nearest(10, 45) 50
63 nearest(25, 328) 325
64 nearest(.1, 4.567) 4.6
65 nearest(10, -45) -50
66
67 nearest_ceil TARGET, LIST
68 Rounds the number(s) to the nearest multiple of the target value.
69 TARGET must be positive. In scalar context, returns a single value;
70 in list context, returns a list of values. Numbers that are halfway
71 between two multiples of the target will be rounded to the ceiling,
72 i.e. the next algebraically higher multiple. For example:
73
74 nearest_ceil(10, 44) yields 40
75 nearest_ceil(10, 45) 50
76 nearest_ceil(10, -45) -40
77
78 nearest_floor TARGET, LIST
79 Rounds the number(s) to the nearest multiple of the target value.
80 TARGET must be positive. In scalar context, returns a single value;
81 in list context, returns a list of values. Numbers that are halfway
82 between two multiples of the target will be rounded to the floor,
83 i.e. the next algebraically lower multiple. For example:
84
85 nearest_floor(10, 44) yields 40
86 nearest_floor(10, 45) 40
87 nearest_floor(10, -45) -50
88
89 nearest_rand TARGET, LIST
90 Rounds the number(s) to the nearest multiple of the target value.
91 TARGET must be positive. In scalar context, returns a single value;
92 in list context, returns a list of values. Numbers that are halfway
93 between two multiples of the target will be rounded up or down in a
94 random fashion. For example, in a large number of trials,
95 "nearest(10, 45)" will yield 40 half the time and 50 half the time.
96
97 nlowmult TARGET, LIST
98 Returns the next lower multiple of the number(s) in LIST. TARGET
99 must be positive. In scalar context, returns a single value; in list
100 context, returns a list of values. Numbers that are between two
101 multiples of the target will be adjusted to the nearest multiples of
102 LIST that are algebraically lower. For example:
103
104 nlowmult(10, 44) yields 40
105 nlowmult(10, 46) 40
106 nlowmult(25, 328) 325
107 nlowmult(.1, 4.567) 4.5
108 nlowmult(10, -41) -50
109
110 nhimult TARGET, LIST
111 Returns the next higher multiple of the number(s) in LIST. TARGET
112 must be positive. In scalar context, returns a single value; in list
113 context, returns a list of values. Numbers that are between two
114 multiples of the target will be adjusted to the nearest multiples of
115 LIST that are algebraically higher. For example:
116
117 nhimult(10, 44) yields 50
118 nhimult(10, 46) 50
119 nhimult(25, 328) 350
120 nhimult(.1, 4.512) 4.6
121 nhimult(10, -49) -40
122
124 The variable $Math::Round::half is used by most routines in this
125 module. Its value is very slightly larger than 0.5, for reasons
126 explained below. If you find that your application does not deliver the
127 expected results, you may reset this variable at will.
128
130 Floating-point numbers are, of course, a rational subset of the real
131 numbers, so calculations with them are not always exact. Numbers that
132 are supposed to be halfway between two others may surprise you; for
133 instance, 0.85 may not be exactly halfway between 0.8 and 0.9, and
134 (0.75 - 0.7) may not be the same as (0.85 - 0.8).
135
136 In order to give more predictable results, these routines use a value
137 for one-half that is slightly larger than 0.5. Nevertheless, if the
138 numbers to be rounded are stored as floating-point, they will be
139 subject, as usual, to the mercies of your hardware, your C compiler,
140 etc.
141
143 Math::Round was written by Geoffrey Rommel <GROMMEL@cpan.org> in
144 October 2000.
145
146
147
148perl v5.12.0 2006-11-21 Round(3)