Skip to content

Commit f4eb9b3

Browse files
committed
- use $sign >= 0 instead of not $sign < 0
- Documentation changes - explain undef as argument to round() and format_number() - see Perl#9
1 parent febbbbf commit f4eb9b3

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

Format.pm

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ calling C<format_negative()> if the number was less than 0.
132132
C<ROUND_OPTION> is only used by C<round()> or C<format_number()>.
133133
-1 or ROUND_FLOOR means floor, -2 or ROUND_ABS_FLOOR means absolute
134134
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.
136138
137139
C<KILO_SUFFIX>, C<MEGA_SUFFIX>, and C<GIGA_SUFFIX> are used by
138140
C<format_bytes()> when the value is over 1024, 1024*1024, or
@@ -189,6 +191,24 @@ use Carp;
189191
use POSIX qw(localeconv);
190192
use base qw(Exporter);
191193

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+
192212
our @EXPORT_SUBS =
193213
qw( format_number format_negative format_picture
194214
format_price format_bytes unformat_number
@@ -256,7 +276,7 @@ our $N_SIGN_POSN = 1; # sign rules for negative: 0-4
256276
our $DECIMAL_DIGITS = 2;
257277
our $DECIMAL_FILL = 0;
258278
our $NEG_FORMAT = '-x';
259-
our $ROUND_OPTION = 0;
279+
our $ROUND_OPTION = ROUND_NORMAL;
260280
our $KILO_SUFFIX = 'K';
261281
our $MEGA_SUFFIX = 'M';
262282
our $GIGA_SUFFIX = 'G';
@@ -311,24 +331,6 @@ our @IGNORE_NEGATIVE = qw( frac_digits int_frac_digits
311331
n_cs_precedes n_sep_by_space n_sign_posn
312332
p_xs_precedes p_sep_by_space p_sign_posn );
313333

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

334336
# INTERNAL FUNCTIONS
@@ -511,20 +513,23 @@ sub new
511513
Rounds the number to the specified precision. If C<$precision> is
512514
omitted, the value of the C<DECIMAL_DIGITS> parameter is used (default
513515
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
528533
529534
Since this is a mathematical rather than string oriented function,
530535
there will be no trailing zeroes to the right of the decimal point,
@@ -595,7 +600,7 @@ sub round
595600
# way floating point numbers work - see string-eq test in t/round.t
596601
if (
597602
$roundoption == ROUND_FLOOR
598-
or ($roundoption == ROUND_ABS_FLOOR and not $sign < 0)
603+
or ($roundoption == ROUND_ABS_FLOOR and $sign >= 0)
599604
or ($roundoption == ROUND_ABS_CEIL and $sign < 0)
600605
) {
601606
if ($sign < 0) {
@@ -615,7 +620,7 @@ sub round
615620
}
616621
elsif (
617622
$roundoption == ROUND_CEIL
618-
or ($roundoption == ROUND_ABS_CEIL and not $sign < 0)
623+
or ($roundoption == ROUND_ABS_CEIL and $sign >= 0)
619624
or ($roundoption == ROUND_ABS_FLOOR and $sign < 0)
620625
) {
621626
if ($sign < 0) {
@@ -722,7 +727,9 @@ set, with a value that is true (not zero, undef, or the empty string).
722727
If C<$precision> is omitted, the value of the C<DECIMAL_DIGITS>
723728
parameter (default value of 2) is used. If C<$mon> is true (default
724729
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.
726733
727734
If the value is too large or great to work with as a regular number,
728735
but instead must be shown in scientific notation, returns that number

0 commit comments

Comments
 (0)