@@ -132,7 +132,9 @@ calling C<format_negative()> if the number was less than 0.
132
132
C<ROUND_OPTION > is only used by C<round() > or C<format_number() > .
133
133
-1 or ROUND_FLOOR means floor, -2 or ROUND_ABS_FLOOR means absolute
134
134
floor, 1 or ROUND_CEIL means ceil, 2 or ROUND_ABS_CEIL means absolute
135
- ceil and a 0 or ROUND_NORMAL stand for normal rounding.
135
+ ceil and a 0 or ROUND_NORMAL means normal rounding. Also see the
136
+ corresponding methods C<floor() > , C<abs_floor() > , C<ceil() > and
137
+ C<abs_ceil() > for more information.
136
138
137
139
C<KILO_SUFFIX > , C<MEGA_SUFFIX > , and C<GIGA_SUFFIX > are used by
138
140
C<format_bytes() > when the value is over 1024, 1024*1024, or
@@ -189,6 +191,24 @@ use Carp;
189
191
use POSIX qw( localeconv) ;
190
192
use base qw( Exporter) ;
191
193
194
+ #
195
+ # Largest integer a 32-bit Perl can handle is based on the mantissa
196
+ # size of a double float, which is up to 53 bits. While we may be
197
+ # able to support larger values on 64-bit systems, some Perl integer
198
+ # operations on 64-bit integer systems still use the 53-bit-mantissa
199
+ # double floats. To be safe, we cap at 2**53; use Math::BigFloat
200
+ # instead for larger numbers.
201
+ #
202
+ use constant MAX_INT => 2**53;
203
+
204
+ #
205
+ # Rounding constants
206
+ use constant ROUND_ABS_FLOOR => -2;
207
+ use constant ROUND_FLOOR => -1;
208
+ use constant ROUND_NORMAL => 0;
209
+ use constant ROUND_CEIL => 1;
210
+ use constant ROUND_ABS_CEIL => 2;
211
+
192
212
our @EXPORT_SUBS =
193
213
qw( format_number format_negative format_picture
194
214
format_price format_bytes unformat_number
@@ -256,7 +276,7 @@ our $N_SIGN_POSN = 1; # sign rules for negative: 0-4
256
276
our $DECIMAL_DIGITS = 2;
257
277
our $DECIMAL_FILL = 0;
258
278
our $NEG_FORMAT = ' -x' ;
259
- our $ROUND_OPTION = 0 ;
279
+ our $ROUND_OPTION = ROUND_NORMAL ;
260
280
our $KILO_SUFFIX = ' K' ;
261
281
our $MEGA_SUFFIX = ' M' ;
262
282
our $GIGA_SUFFIX = ' G' ;
@@ -311,24 +331,6 @@ our @IGNORE_NEGATIVE = qw( frac_digits int_frac_digits
311
331
n_cs_precedes n_sep_by_space n_sign_posn
312
332
p_xs_precedes p_sep_by_space p_sign_posn ) ;
313
333
314
- #
315
- # Largest integer a 32-bit Perl can handle is based on the mantissa
316
- # size of a double float, which is up to 53 bits. While we may be
317
- # able to support larger values on 64-bit systems, some Perl integer
318
- # operations on 64-bit integer systems still use the 53-bit-mantissa
319
- # double floats. To be safe, we cap at 2**53; use Math::BigFloat
320
- # instead for larger numbers.
321
- #
322
- use constant MAX_INT => 2**53;
323
-
324
- #
325
- # Rounding constants
326
- use constant ROUND_ABS_FLOOR => -2;
327
- use constant ROUND_FLOOR => -1;
328
- use constant ROUND_NORMAL => 0;
329
- use constant ROUND_CEIL => 1;
330
- use constant ROUND_ABS_CEIL => 2;
331
-
332
334
# ##---------------------------------------------------------------------
333
335
334
336
# INTERNAL FUNCTIONS
@@ -511,20 +513,23 @@ sub new
511
513
Rounds the number to the specified precision. If C<$precision > is
512
514
omitted, the value of the C<DECIMAL_DIGITS > parameter is used (default
513
515
value 2). Both input and output are numeric (the function uses math
514
- operators rather than string manipulation to do its job), The value of
515
- C<$precision > may be any integer, positive or negative. If C<$roundoption >
516
- is omitted, the value of the C<ROUND_OPTION > paramter is used (default
517
- value 0). Examples:
518
-
519
- round(3.14159) yields 3.14
520
- round(3.14159, undef, 1) yields 3.15
521
- round(3.14159, undef, -1) yields 3.14
522
- round(3.14159, 4) yields 3.1416
523
- round(42.00, 4) yields 42
524
- round(1234, -2) yields 1200
525
- round(1234, -2, ROUND_CEIL) yields 1300
526
- round(1298, -2) yields 1300
527
- round(1298, -2, ROUND_FLOOR) yields 1200
516
+ operators rather than string manipulation to do its job), The value
517
+ of C<$precision > may be any integer, positive or negative. If
518
+ C<$roundoption > is omitted, the value of the C<ROUND_OPTION > paramter is
519
+ used (default value C<ROUND_NORMAL > ). Passing undef as a value for
520
+ C<$precision > or C<$roundoption > will preserve the default behavior.
521
+
522
+ Examples:
523
+
524
+ round(3.14159) yields 3.14
525
+ round(3.14159, undef, ROUND_CEIL) yields 3.15
526
+ round(3.14159, undef, ROUND_FLOOR) yields 3.14
527
+ round(3.14159, 4) yields 3.1416
528
+ round(42.00, 4) yields 42
529
+ round(1234, -2) yields 1200
530
+ round(1234, -2, ROUND_CEIL) yields 1300
531
+ round(1298, -2) yields 1300
532
+ round(1298, -2, ROUND_FLOOR) yields 1200
528
533
529
534
Since this is a mathematical rather than string oriented function,
530
535
there will be no trailing zeroes to the right of the decimal point,
@@ -595,7 +600,7 @@ sub round
595
600
# way floating point numbers work - see string-eq test in t/round.t
596
601
if (
597
602
$roundoption == ROUND_FLOOR
598
- or ($roundoption == ROUND_ABS_FLOOR and not $sign < 0)
603
+ or ($roundoption == ROUND_ABS_FLOOR and $sign >= 0)
599
604
or ($roundoption == ROUND_ABS_CEIL and $sign < 0)
600
605
) {
601
606
if ($sign < 0) {
@@ -615,7 +620,7 @@ sub round
615
620
}
616
621
elsif (
617
622
$roundoption == ROUND_CEIL
618
- or ($roundoption == ROUND_ABS_CEIL and not $sign < 0)
623
+ or ($roundoption == ROUND_ABS_CEIL and $sign >= 0)
619
624
or ($roundoption == ROUND_ABS_FLOOR and $sign < 0)
620
625
) {
621
626
if ($sign < 0) {
@@ -722,7 +727,9 @@ set, with a value that is true (not zero, undef, or the empty string).
722
727
If C<$precision > is omitted, the value of the C<DECIMAL_DIGITS >
723
728
parameter (default value of 2) is used. If C<$mon > is true (default
724
729
value false) the monetary separators are used. C<$precision > and
725
- C<$roundoption > are direct passed to C<round() > .
730
+ C<$roundoption > are direct passed to C<round() > . Passing undef as a
731
+ value for C<$precision > , C<$trailing_zeroes > , C<$mon > or C<$roundoption >
732
+ will preserve the default behavior.
726
733
727
734
If the value is too large or great to work with as a regular number,
728
735
but instead must be shown in scientific notation, returns that number
0 commit comments