| 1 | package Number::Format 1.79; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl extension for formatting numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use v5.12.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod use Number::Format; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod my $x = Number::Format->new(%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod $formatted = $x->round($number, $precision); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod $formatted = $x->format_number($number, $precision, $trailing_zeroes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod $formatted = $x->format_negative($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod $formatted = $x->format_picture($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod $formatted = $x->format_price($number, $precision, $symbol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod $formatted = $x->format_bytes($number, $precision); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod $number = $x->unformat_number($formatted); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod use Number::Format qw(:subs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod $formatted = round($number, $precision); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod $formatted = format_number($number, $precision, $trailing_zeroes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod $formatted = format_negative($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod $formatted = format_picture($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod $formatted = format_price($number, $precision, $symbol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod $formatted = format_bytes($number, $precision); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod $number = unformat_number($formatted); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod These functions provide an easy means of formatting numbers in a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod manner suitable for displaying to the user. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod There are two ways to use this package. One is to declare an object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod of type Number::Format, which you can think of as a formatting engine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod The various functions defined here are provided as object methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod The constructor C<new()> can be used to set the parameters of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod formatting engine. Valid parameters are: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod THOUSANDS_SEP - character inserted between groups of 3 digits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod DECIMAL_POINT - character separating integer and fractional parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod INT_CURR_SYMBOL - character(s) denoting currency (see format_price()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod DECIMAL_DIGITS - number of digits to the right of dec point (def 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod NEG_FORMAT - format to display negative numbers (def ``-x'') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod KILO_SUFFIX - suffix to add when format_bytes formats kilobytes (trad) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod MEGA_SUFFIX - " " " " " " megabytes (trad) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod GIGA_SUFFIX - " " " " " " gigabytes (trad) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod TERA_SUFFIX - " " " " " " terabytes (trad) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod KIBI_SUFFIX - suffix to add when format_bytes formats kibibytes (iec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod MEBI_SUFFIX - " " " " " " mebibytes (iec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | #pod GIBI_SUFFIX - " " " " " " gibibytes (iec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | #pod TEBI_SUFFIX - " " " " " " tebibytes (iec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod They may be specified in upper or lower case, with or without a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | #pod leading hyphen ( - ). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | #pod If C<THOUSANDS_SEP> is set to the empty string, format_number will not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | #pod insert any separators. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | #pod The defaults for C<THOUSANDS_SEP>, C<DECIMAL_POINT>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | #pod C<MON_THOUSANDS_SEP>, C<MON_DECIMAL_POINT>, and C<INT_CURR_SYMBOL> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | #pod come from the POSIX locale information (see L<perllocale>). If your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | #pod POSIX locale does not provide C<MON_THOUSANDS_SEP> and/or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | #pod C<MON_DECIMAL_POINT> fields, then the C<THOUSANDS_SEP> and/or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | #pod C<DECIMAL_POINT> values are used for those parameters. Formerly, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | #pod POSIX was optional but this caused problems in some cases, so it is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | #pod now required. If this causes you hardship, please contact the author | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | #pod of this package at <SwPrAwM@cpan.org> (remove "SPAM" to get correct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | #pod email address) for help. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | #pod If any of the above parameters are not specified when you invoke | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | #pod C<new()>, the values are taken from the current locale, and failing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod that from these built-in defaults: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod THOUSANDS_SEP = ',' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod DECIMAL_POINT = '.' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod MON_THOUSANDS_SEP = ',' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | #pod MON_DECIMAL_POINT = '.' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod INT_CURR_SYMBOL = 'USD' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod DECIMAL_DIGITS = 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod DECIMAL_FILL = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod NEG_FORMAT = '-x' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | #pod KILO_SUFFIX = 'K' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod MEGA_SUFFIX = 'M' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod GIGA_SUFFIX = 'G' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | #pod TERA_SUFFIX = 'T' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod KIBI_SUFFIX = 'KiB' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod MEBI_SUFFIX = 'MiB' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | #pod GIBI_SUFFIX = 'GiB' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | #pod TEBI_SUFFIX = 'TiB' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | #pod Note however that when you first call one of the functions in this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | #pod module I<without> using the object-oriented interface, further setting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | #pod of those global variables will have no effect on non-OO calls. It is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | #pod recommended that you use the object-oriented interface instead for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | #pod fewer headaches and a cleaner design. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | #pod The C<DECIMAL_FILL> and C<DECIMAL_DIGITS> values are not set by the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | #pod Locale system, but are definable by the user. They affect the output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | #pod of C<format_number()>. Setting C<DECIMAL_DIGITS> is like giving that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | #pod value as the C<$precision> argument to that function. Setting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | #pod C<DECIMAL_FILL> to a true value causes C<format_number()> to append | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | #pod zeroes to the right of the decimal digits until the length is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | #pod specified number of digits. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | #pod C<NEG_FORMAT> is only used by C<format_negative()> and is a string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | #pod containing the letter 'x', where that letter will be replaced by a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | #pod positive representation of the number being passed to that function. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | #pod C<format_number()> and C<format_price()> utilize this feature by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | #pod calling C<format_negative()> if the number was less than 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | #pod C<KILO_SUFFIX>, C<MEGA_SUFFIX>, C<GIGA_SUFFIX>, and C<TERA_SUFFIX> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | #pod used by C<format_bytes()> when the value is over 1024, 1024**2, 1024**3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | #pod or 1024**4, respectively. The default values are "K", "M", "G", and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | #pod "T". These apply in the default "traditional" mode only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | #pod C<KIBI_SUFFIX>, C<MEBI_SUFFIX>, C<GIBI_SUFFIX>, and C<TEBI_SUFFIX> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | #pod used by C<format_bytes()> when the value is over 1024, 1024**2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | #pod 1024**3, or 1024**4, respectively. The default values are "KiB", "MiB", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | #pod "GiB", "TiB". These apply in the "iso60027" mode only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | #pod The only restrictions on C<DECIMAL_POINT> and C<THOUSANDS_SEP> are that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | #pod they must not be digits and must not be identical. There are no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | #pod restrictions on C<INT_CURR_SYMBOL>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | #pod For example, a German user might include this in their code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | #pod use Number::Format; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | #pod my $de = Number::Format->new(-thousands_sep => '.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | #pod -decimal_point => ',', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | #pod -int_curr_symbol => 'DEM'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | #pod my $formatted = $de->format_number($number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | #pod Or, if you prefer not to use the object oriented interface, you can call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | #pod the functions directly: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | #pod use Number::Format qw(:subs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | #pod my $formatted = format_number($number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | #pod In that case the parameters come from the current locale and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | #pod built-in defaults, and there is no way to override them. If you need | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | #pod non-default parameters, use the object oriented interface. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | #pod =head1 EXPORTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | #pod Nothing is exported by default. To export functions, name them on the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | #pod import list of the C<use Number::Format> statement, or use the tag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | #pod C<:subs> to export all of them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | #pod The tag C<:vars> (and the variables half of C<:all>) still exists for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | #pod compatibility, but it no longer has any useful purpose. It exports a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | #pod set of package variables that were once the way to configure the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | #pod functional interface; assigning to them has had no effect since version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | #pod 1.60 in 2008. Use C<new()> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | ###--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 9 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 9 | ▶ | use POSIX qw(localeconv); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 9 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 9 | our @EXPORT_SUBS = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | qw( format_number format_negative format_picture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | format_price format_bytes round unformat_number ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 9 | our @EXPORT_LC_NUMERIC = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | qw( $DECIMAL_POINT $THOUSANDS_SEP $GROUPING ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | our @EXPORT_LC_MONETARY = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | qw( $INT_CURR_SYMBOL $CURRENCY_SYMBOL $MON_DECIMAL_POINT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | $MON_THOUSANDS_SEP $MON_GROUPING $POSITIVE_SIGN $NEGATIVE_SIGN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | $INT_FRAC_DIGITS $FRAC_DIGITS $P_CS_PRECEDES $P_SEP_BY_SPACE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | $N_CS_PRECEDES $N_SEP_BY_SPACE $P_SIGN_POSN $N_SIGN_POSN ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 9 | our @EXPORT_OTHER = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | qw( $DECIMAL_DIGITS $DECIMAL_FILL $NEG_FORMAT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | $KILO_SUFFIX $MEGA_SUFFIX $GIGA_SUFFIX $TERA_SUFFIX | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | $KIBI_SUFFIX $MEBI_SUFFIX $GIBI_SUFFIX $TEBI_SUFFIX); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 9 | our @EXPORT_VARS = ( @EXPORT_LC_NUMERIC, @EXPORT_LC_MONETARY, @EXPORT_OTHER ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 9 | our @EXPORT_ALL = ( @EXPORT_SUBS, @EXPORT_VARS ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 9 | our @EXPORT_OK = ( @EXPORT_ALL ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 9 | our %EXPORT_TAGS = ( subs => \@EXPORT_SUBS, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | vars => \@EXPORT_VARS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | lc_numeric_vars => \@EXPORT_LC_NUMERIC, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | lc_monetary_vars => \@EXPORT_LC_MONETARY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | other_vars => \@EXPORT_OTHER, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | all => \@EXPORT_ALL ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Refer to http://www.opengroup.org/onlinepubs/007908775/xbd/locale.html | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # for more details about the POSIX variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # Locale variables provided by POSIX for numbers (LC_NUMERIC) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 9 | our $DECIMAL_POINT = '.'; # decimal point symbol for numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 9 | our $THOUSANDS_SEP = ','; # thousands separator for numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 9 | our $GROUPING = undef;# grouping rules for thousands (UNSUPPORTED) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # Locale variables provided by POSIX for currency (LC_MONETARY) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 9 | our $INT_CURR_SYMBOL = 'USD';# intl currency symbol | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 9 | our $CURRENCY_SYMBOL = '$'; # domestic currency symbol | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 9 | our $MON_DECIMAL_POINT = '.'; # decimal point symbol for monetary values | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 9 | our $MON_THOUSANDS_SEP = ','; # thousands separator for monetary values | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 9 | our $MON_GROUPING = undef;# like 'grouping' for monetary (UNSUPPORTED) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 9 | our $POSITIVE_SIGN = ''; # string to add for non-negative monetary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 9 | our $NEGATIVE_SIGN = '-'; # string to add for negative monetary | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | our $INT_FRAC_DIGITS = 2; # digits to right of decimal for intl currency | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 9 | our $FRAC_DIGITS = 2; # digits to right of decimal for currency | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 9 | our $P_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) positive | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 9 | our $P_SEP_BY_SPACE = 1; # add space to positive; 0, 1, or 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 9 | our $N_CS_PRECEDES = 1; # curr sym precedes(1) or follows(0) negative | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 9 | our $N_SEP_BY_SPACE = 1; # add space to negative; 0, 1, or 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 9 | our $P_SIGN_POSN = 1; # sign rules for positive: 0-4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 9 | our $N_SIGN_POSN = 1; # sign rules for negative: 0-4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # The following are specific to Number::Format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 9 | our $DECIMAL_DIGITS = 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 9 | our $DECIMAL_FILL = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 9 | our $NEG_FORMAT = '-x'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 9 | our $KILO_SUFFIX = 'K'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 9 | our $MEGA_SUFFIX = 'M'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 9 | our $GIGA_SUFFIX = 'G'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 9 | our $TERA_SUFFIX = 'T'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 9 | our $KIBI_SUFFIX = 'KiB'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 9 | our $MEBI_SUFFIX = 'MiB'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 9 | our $GIBI_SUFFIX = 'GiB'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 9 | our $TEBI_SUFFIX = 'TiB'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 9 | our $DEFAULT_LOCALE = { ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # LC_NUMERIC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | decimal_point => $DECIMAL_POINT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | thousands_sep => $THOUSANDS_SEP, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | grouping => $GROUPING, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # LC_MONETARY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | int_curr_symbol => $INT_CURR_SYMBOL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | currency_symbol => $CURRENCY_SYMBOL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | mon_decimal_point => $MON_DECIMAL_POINT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | mon_thousands_sep => $MON_THOUSANDS_SEP, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | mon_grouping => $MON_GROUPING, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | positive_sign => $POSITIVE_SIGN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | negative_sign => $NEGATIVE_SIGN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | int_frac_digits => $INT_FRAC_DIGITS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | frac_digits => $FRAC_DIGITS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | p_cs_precedes => $P_CS_PRECEDES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | p_sep_by_space => $P_SEP_BY_SPACE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | n_cs_precedes => $N_CS_PRECEDES, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | n_sep_by_space => $N_SEP_BY_SPACE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | p_sign_posn => $P_SIGN_POSN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | n_sign_posn => $N_SIGN_POSN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # The following are specific to Number::Format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | decimal_digits => $DECIMAL_DIGITS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | decimal_fill => $DECIMAL_FILL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | neg_format => $NEG_FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | kilo_suffix => $KILO_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | mega_suffix => $MEGA_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | giga_suffix => $GIGA_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | tera_suffix => $TERA_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | kibi_suffix => $KIBI_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | mebi_suffix => $MEBI_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | gibi_suffix => $GIBI_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | tebi_suffix => $TEBI_SUFFIX, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # POSIX::localeconv() returns -1 for numeric values that are not applicable to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # the current locale. This module ignores them. @IGNORE_NEGATIVE lists the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | # ones that this module otherwise handles (there are some fields that this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # module always ignores, so don't need to be in the list). (Prior to v5.37.7, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # only the Windows version of POSIX::localeconv() returned -1; other versions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # simply didn't return any values at all for not-applicable fields. But the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # end result is the same regardless of version.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 9 | our @IGNORE_NEGATIVE = qw( frac_digits int_frac_digits | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | n_cs_precedes n_sep_by_space n_sign_posn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | p_xs_precedes p_sep_by_space p_sign_posn ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # Largest integer a 32-bit Perl can handle is based on the mantissa | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # size of a double float, which is up to 53 bits. While we may be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # able to support larger values on 64-bit systems, some Perl integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # operations on 64-bit integer systems still use the 53-bit-mantissa | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # double floats. To be safe, we cap at 2**53; use Math::BigFloat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # instead for larger numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 9 | ▶ | use constant MAX_INT => 2**53; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | ###--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # INTERNAL FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # These functions (with names beginning with '_' are for internal use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # only. There is no guarantee that they will remain the same from one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # version to the next! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # _get_self creates an instance of Number::Format with the default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # values for the configuration parameters, if the first element of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # @_ is not already an object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 9 | my $DefaultObject; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | sub _get_self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 349 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_self: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # Not calling $_[0]->isa because that may result in unblessed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # reference error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 349 | ▶ | unless (ref $_[0] && UNIVERSAL::isa($_[0], "Number::Format")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] and UNIVERSAL::isa($_[0], "Number::Format")
MC/DC50%1/2
ref $_[0] and UNIVERSAL::isa($_[0], "Number::Format")
ref $_[0]
UNIVERSAL::isa($_[0], "Number::Format")
Truth table66%2/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "Number::Format")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 71 | ▶ | $DefaultObject ||= Number::Format->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$DefaultObject ||= "Number::Format"->new
MC/DC0%0/2
$DefaultObject ||= "Number::Format"->new
$DefaultObject
"Number::Format"->new
Truth table66%2/3
A ||= B
A= $DefaultObjectB= "Number::Format"->new
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 71 | unshift (@_, $DefaultObject); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 349 | @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # _check_seps is used to validate that the thousands_sep, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # decimal_point, mon_thousands_sep and mon_decimal_point variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # have acceptable values. For internal use only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | sub _check_seps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 135 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_seps: called CC=9 SCAR=27.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 135 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 135 | ▶ | croak "Not an object" unless ref $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 135 | foreach my $prefix ("", "mon_") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | croak "${prefix}thousands_sep is undefined" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 270 | ▶ | unless defined $self->{"${prefix}thousands_sep"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | croak "${prefix}thousands_sep may not be numeric" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 270 | ▶ | if $self->{"${prefix}thousands_sep"} =~ /\d/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | croak "${prefix}decimal_point may not be numeric" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 270 | ▶ | if $self->{"${prefix}decimal_point"} =~ /\d/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | croak("${prefix}thousands_sep and ". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | "${prefix}decimal_point may not be equal") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | if $self->{"${prefix}decimal_point"} eq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | $self->{"${prefix}thousands_sep"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # There are legal locales where 'mon_decimal_point' and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # 'mon_thousands_sep' are both "" (the empty string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 270 | ▶ | && ($prefix eq "" || $self->{"mon_decimal_point"} ne ""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$$self{"${prefix}decimal_point"} eq $$self{"${prefix}thousands_sep"} and $prefix eq "" || $self->{"mon_decimal_point"} ne ""
$prefix eq "" or $self->{"mon_decimal_point"} ne ""
MC/DC0%0/3
$$self{"${prefix}decimal_point"} eq $$self{"${prefix}thousands_sep"} and $prefix eq "" || $self->{"mon_decimal_point"} ne ""
$$self{"${prefix}decimal_point"} eq $$self{"${prefix}thousands_sep"}
$prefix eq ""
$self->{"mon_decimal_point"} ne ""
Truth table25%1/4
A and B or C
A= $$self{"${prefix}decimal_point"} eq $$self{"${prefix}thousands_sep"}B= $prefix eq ""C= $self->{"mon_decimal_point"} ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # _get_multipliers returns the multipliers to be used for kilo, mega, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # giga, and tera (un-)formatting. Used in format_bytes and unformat_number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # For internal use only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | sub _get_multipliers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 43 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_multipliers: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 43 | my($base) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 43 | ▶ | if (!defined($base) || $base == 1024) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not defined $base or $base == 1024
MC/DC100%2/2
not defined $base or $base == 1024
not defined $base
$base == 1024
Truth table100%3/3
not(A) or B
A= not defined $baseB= $base == 1024
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # the "tera" value is not portable, but that's a matter for the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 9 | ▶ | no warnings 'portable'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 34 | return ( kilo => 0x00000000400, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | mega => 0x00000100000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | giga => 0x00040000000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | tera => 0x10000000000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | elsif ($base == 1000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 4 | return ( kilo => 1_000, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | mega => 1_000_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | giga => 1_000_000_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | tera => 1_000_000_000_000 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 5 | ▶ | croak "base overflow" if $base **3 > MAX_INT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 4 | ▶ | croak "base must be a positive integer" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$base > 0 and $base == int $base
MC/DC100%2/2
$base > 0 and $base == int $base
$base > 0
$base == int $base
Truth table100%3/3
A and B
A= $base > 0B= $base == int $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | unless $base > 0 && $base == int($base); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | return ( kilo => $base, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | mega => $base ** 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | giga => $base ** 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | tera => $base ** 4); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # _complain_undef displays a warning message on STDERR and is called | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # when a subroutine has been invoked with an undef value. A warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # message is printed if the calling environment has "uninitialized" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | # warnings enabled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | sub _complain_undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_complain_undef: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 8 | my @stack; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 8 | my($sub, $bitmask) = (caller(1))[3,9]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 8 | my $offset = $warnings::Offsets{"uninitialized"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 8 | ▶ | carp "Use of uninitialized value in call to $sub" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | if vec($bitmask, $offset, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | #pod =method new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | #pod my $formatter = Number::Format->new( %args ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | #pod Creates a new Number::Format object. Valid keys for %args are any of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | #pod the parameters described above. Keys may be in all uppercase or all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | #pod lowercase, and may optionally be preceded by a hyphen (-) character. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | #pod Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | #pod my $de = Number::Format->new(-thousands_sep => '.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | #pod -decimal_point => ',', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | #pod -int_curr_symbol => 'DEM'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 13 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 13 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 13 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # Fetch defaults from current locale, or failing that, using globals | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 13 | my $me = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # my $locale = setlocale(LC_ALL, ""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 13 | my $locale_values = localeconv(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # Strip out illegal negative values from the current locale | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 13 | foreach ( @IGNORE_NEGATIVE ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 104 | ▶ | if (defined($locale_values->{$_}) && $locale_values->{$_} eq '-1') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $locale_values->{$_} and $locale_values->{$_} eq -1
MC/DC50%1/2
defined $locale_values->{$_} and $locale_values->{$_} eq -1
defined $locale_values->{$_}
$locale_values->{$_} eq -1
Truth table66%2/3
A and B
A= defined $locale_values->{$_}B= $locale_values->{$_} eq -1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 91 | delete $locale_values->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 13 | ▶ | while(my($arg, $default) = each %$DEFAULT_LOCALE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | $me->{$arg} = (( exists $locale_values->{$arg}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | && $locale_values->{$arg} ne "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 377 | ▶ | ? $locale_values->{$arg} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $locale_values->{$arg} and $locale_values->{$arg} ne ""
MC/DC100%2/2
exists $locale_values->{$arg} and $locale_values->{$arg} ne ""
exists $locale_values->{$arg}
$locale_values->{$arg} ne ""
Truth table100%3/3
A and B
A= exists $locale_values->{$arg}B= $locale_values->{$arg} ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | : $default; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 377 | foreach ($arg, uc $arg, "-$arg", uc "-$arg") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1465 | ▶ | next unless defined $args{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 41 | $me->{$arg} = $args{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 41 | delete $args{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 41 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # Some locales set the decimal_point to "," and the thousands_sep to "". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # This module generally defaults an empty thousands_sep to ",", creating a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # conflict in such a locale. Instead, leave the thousands_sep as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # empty string. Suggested by Moritz Onken. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 13 | foreach my $prefix ("", "mon_") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | $me->{"${prefix}thousands_sep"} = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | if ($me->{"${prefix}decimal_point"} eq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 26 | ▶ | $me->{"${prefix}thousands_sep"}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 13 | ▶ | croak "Invalid argument(s)" if %args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 13 | bless $me, $type; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 13 | $me; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | #pod =method round | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | #pod my $str = $formatter->($number, $precision); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | #pod Rounds the number to the specified precision. If C<$precision> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | #pod omitted, the value of the C<DECIMAL_DIGITS> parameter is used (default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | #pod value 2). Both input and output are numeric (the function uses math | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | #pod operators rather than string manipulation to do its job), The value of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | #pod C<$precision> may be any integer, positive or negative. Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | #pod round(3.14159) yields 3.14 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | #pod round(3.14159, 4) yields 3.1416 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | #pod round(42.00, 4) yields 42 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | #pod round(1234, -2) yields 1200 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | #pod Since this is a mathematical rather than string oriented function, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | #pod there will be no trailing zeroes to the right of the decimal point, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | #pod and the C<DECIMAL_POINT> and C<THOUSANDS_SEP> variables are ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | #pod To format your number using the C<DECIMAL_POINT> and C<THOUSANDS_SEP> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | #pod variables, use C<format_number()> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | sub round | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 128 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
round: called CC=9 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 128 | my ($self, $number, $precision) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 128 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 128 | ▶ | $precision = $self->{decimal_digits} unless defined $precision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 128 | ▶ | $precision = 2 unless defined $precision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 128 | ▶ | croak("precision must be integer") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | unless int($precision) == $precision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 128 | ▶ | if (ref($number) && $number->isa("Math::BigFloat")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $number and $number->isa("Math::BigFloat")
MC/DC0%0/2
ref $number and $number->isa("Math::BigFloat")
ref $number
$number->isa("Math::BigFloat")
Truth table33%1/3
A and B
A= ref $numberB= $number->isa("Math::BigFloat")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | my $rounded = $number->copy(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | $rounded->precision(-$precision); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | return $rounded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 128 | my $sign = $number <=> 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 128 | my $multiplier = (10 ** $precision); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 128 | my $result = abs($number); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 128 | my $product = $result * $multiplier; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 128 | ▶ | croak "round() overflow. Try smaller precision or use Math::BigFloat" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | if $product > MAX_INT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # We need to add 1e-14 to avoid some rounding errors due to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | # way floating point numbers work - see string-eq test in t/round.t | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 127 | $result = int($product + .5 + 1e-14) / $multiplier; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 127 | ▶ | $result = -$result if $sign < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 127 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | #pod =method format_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | #pod my $str = $formatter->format_number($number, $precision, $trailing_zeroes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | #pod Formats a number by adding C<THOUSANDS_SEP> between each set of 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | #pod digits to the left of the decimal point, substituting C<DECIMAL_POINT> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | #pod for the decimal point, and rounding to the specified precision using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | #pod C<round()>. Note that C<$precision> is a I<maximum> precision | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | #pod specifier; trailing zeroes will only appear in the output if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | #pod C<$trailing_zeroes> is provided, or the parameter C<DECIMAL_FILL> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | #pod set, with a value that is true (not zero, undef, or the empty string). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | #pod If C<$precision> is omitted, the value of the C<DECIMAL_DIGITS> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | #pod parameter (default value of 2) is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | #pod If the value is too large or great to work with as a regular number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | #pod but instead must be shown in scientific notation, returns that number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | #pod in scientific notation without further formatting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | #pod Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | #pod format_number(12345.6789) yields '12,345.68' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | #pod format_number(123456.789, 2) yields '123,456.79' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | #pod format_number(1234567.89, 2) yields '1,234,567.89' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | #pod format_number(1234567.8, 2) yields '1,234,567.8' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | #pod format_number(1234567.8, 2, 1) yields '1,234,567.80' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | #pod format_number(1.23456789, 6) yields '1.234568' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | #pod format_number("0.000020000E+00", 7);' yields '2e-05' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | #pod Of course the output would have your values of C<THOUSANDS_SEP> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | #pod C<DECIMAL_POINT> instead of ',' and '.' respectively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | sub format_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 97 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_number: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 97 | my ($self, $number, $precision, $trailing_zeroes, $mon) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 97 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 2 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 2 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 97 | $self->_check_seps(); # first make sure the SEP variables are valid | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | my($thousands_sep, $decimal_point) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | $mon ? @$self{qw(mon_thousands_sep mon_decimal_point)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 97 | ▶ | : @$self{qw(thousands_sep decimal_point)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | # Set defaults and standardize number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 97 | ▶ | $precision = $self->{decimal_digits} unless defined $precision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 97 | ▶ | $trailing_zeroes = $self->{decimal_fill} unless defined $trailing_zeroes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | # Handle negative numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 97 | my $sign = $number <=> 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 97 | ▶ | $number = abs($number) if $sign < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 97 | $number = $self->round($number, $precision); # round off $number | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | # detect scientific notation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 96 | my $exponent = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 96 | ▶ | if ($number =~ /^(-?[\d.]+)e([+-]\d+)$/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | # Don't attempt to format numbers that require scientific notation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 1 | ▶ | return ($sign < 0) ? $self->format_negative($number) : $number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # Split integer and decimal parts of the number and add commas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 95 | my $integer = int($number); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 95 | my $decimal; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | # Note: In perl 5.6 and up, string representation of a number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | # automagically includes the locale decimal point. This way we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | # will detect the decimal part correctly as long as the decimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | # point is 1 character. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 95 | ▶ | $decimal = substr($number, length($integer)+1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | if (length($integer) < length($number)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 95 | ▶ | $decimal = '' unless defined $decimal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | # Add trailing 0's if $trailing_zeroes is set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 95 | ▶ | $decimal .= '0'x( $precision - length($decimal) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$trailing_zeroes and $precision > length $decimal
MC/DC100%2/2
$trailing_zeroes and $precision > length $decimal
$trailing_zeroes
$precision > length $decimal
Truth table100%3/3
A and B
A= $trailing_zeroesB= $precision > length $decimal
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | if $trailing_zeroes && $precision > length($decimal); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | # Add the commas (or whatever is in thousands_sep). If | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | # thousands_sep is the empty string, do nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 95 | ▶ | if ($thousands_sep) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | # Add leading 0's so length($integer) is divisible by 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 95 | $integer = '0'x(3 - (length($integer) % 3)).$integer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # Split $integer into groups of 3 characters and insert commas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | $integer = join($thousands_sep, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 95 | grep {$_ ne ''} split(/(...)/, $integer)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | # Strip off leading zeroes and optional thousands separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 95 | $integer =~ s/^0+(?:\Q$thousands_sep\E)?//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 95 | ▶ | $integer = '0' if $integer eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | # Combine integer and decimal parts and return the result. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 95 | ▶ | my $result = ((defined $decimal && length $decimal) ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $decimal and length $decimal
MC/DC50%1/2
defined $decimal and length $decimal
defined $decimal
length $decimal
Truth table66%2/3
A and B
A= defined $decimalB= length $decimal
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | join($decimal_point, $integer, $decimal) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | $integer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 95 | ▶ | return ($sign < 0) ? $self->format_negative($result) : $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | #pod =method format_negative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | #pod my $str = $formatter->($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | #pod Formats a negative number. Picture should be a string that contains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | #pod the letter C<x> where the number should be inserted. For example, for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | #pod standard negative numbers you might use ``C<-x>'', while for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | #pod accounting purposes you might use ``C<(x)>''. If the specified number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | #pod begins with a ``-'' character, that will be removed before formatting, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | #pod but formatting will occur whether or not the number is negative. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | sub format_negative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_negative: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 9 | my($self, $number, $format) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 9 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 1 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 9 | ▶ | $format = $self->{neg_format} unless defined $format; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 9 | ▶ | croak "Letter x must be present in picture in format_negative()" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | unless $format =~ /x/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 9 | $number =~ s/^-//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 9 | $format =~ s/x/$number/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 9 | return $format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | #pod =method format_picture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | #pod my $str = $formatter->format_picture($number, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | #pod Returns a string based on C<$picture> with the C<#> characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | #pod replaced by digits from C<$number>. If the length of the integer part | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | #pod of $number is too large to fit, the C<#> characters are replaced with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | #pod asterisks (C<*>) instead. Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | #pod format_picture(100.023, 'USD ##,###.##') yields 'USD 100.02' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | #pod format_picture(1000.23, 'USD ##,###.##') yields 'USD 1,000.23' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | #pod format_picture(10002.3, 'USD ##,###.##') yields 'USD 10,002.30' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | #pod format_picture(100023, 'USD ##,###.##') yields 'USD **,***.**' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | #pod format_picture(1.00023, 'USD #.###,###') yields 'USD 1.002,300' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | #pod The comma (,) and period (.) you see in the picture examples should | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | #pod match the values of C<THOUSANDS_SEP> and C<DECIMAL_POINT>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | #pod respectively, for proper operation. However, the C<THOUSANDS_SEP> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | #pod characters in C<$picture> need not occur every three digits; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | #pod I<only> use of that variable by this function is to remove leading | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | #pod commas (see the first example above). There may not be more than one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | #pod instance of C<DECIMAL_POINT> in C<$picture>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | #pod The value of C<NEG_FORMAT> is used to determine how negative numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | #pod are displayed. The result of this is that the output of this function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | #pod my have unexpected spaces before and/or after the number. This is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | #pod necessary so that positive and negative numbers are formatted into a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | #pod space the same size. If you are only using positive numbers and want | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | #pod to avoid this problem, set NEG_FORMAT to "x". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | sub format_picture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_picture: called CC=25 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 15 | my ($self, $number, $picture) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 15 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 1 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 15 | ▶ | croak "Picture not defined" unless defined($picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 15 | $self->_check_seps(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | # Handle negative numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | # A neg_format like "x" has no prefix, and the match fails, so default to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | # the empty string rather than warning about undef. (RT#148306) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | # -- claude, 2026-09-16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 15 | my($neg_prefix) = $self->{neg_format} =~ /^([^x]+)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | 15 | ▶ | $neg_prefix = '' unless defined $neg_prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 15 | my($pic_prefix) = $picture =~ /^([^\#]+)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 15 | my $neg_pic = $self->{neg_format}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 15 | (my $pos_pic = $self->{neg_format}) =~ s/[^x\s]/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 15 | (my $pos_prefix = $neg_prefix) =~ s/[^x\s]/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 15 | $neg_pic =~ s/x/$picture/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | 15 | $pos_pic =~ s/x/$picture/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 15 | my $sign = $number <=> 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 15 | ▶ | $number = abs($number) if $sign < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 15 | ▶ | $picture = $sign < 0 ? $neg_pic : $pos_pic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 15 | ▶ | my $sign_prefix = $sign < 0 ? $neg_prefix : $pos_prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | # Split up the picture and die if there is more than one $DECIMAL_POINT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 15 | my($pic_int, $pic_dec, @cruft) = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | split(/\Q$self->{decimal_point}\E/, $picture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 15 | ▶ | $pic_int = '' unless defined $pic_int; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 15 | ▶ | $pic_dec = '' unless defined $pic_dec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 15 | ▶ | croak "Only one decimal separator permitted in picture" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | if @cruft; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | # Obtain precision from the length of the decimal part... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 15 | my $precision = $pic_dec; # start with copying it | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 15 | $precision =~ s/[^\#]//g; # eliminate all non-# characters | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 15 | $precision = length $precision; # take the length of the result | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | # Format the number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 15 | $number = $self->round($number, $precision); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | # Obtain the length of the integer portion just like we did for $precision | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 15 | my $intsize = $pic_int; # start with copying it | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 15 | $intsize =~ s/[^\#]//g; # eliminate all non-# characters | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 15 | $intsize = length $intsize; # take the length of the result | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | # Split up $number same as we did for $picture earlier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 15 | my($num_int, $num_dec) = split(/\./, $number, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 15 | ▶ | $num_int = '' unless defined $num_int; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 15 | ▶ | $num_dec = '' unless defined $num_dec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | # Check if the integer part will fit in the picture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 15 | ▶ | if (length $num_int > $intsize) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 2 | $picture =~ s/\#/\*/g; # convert # to * and return it | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 2 | ▶ | $pic_prefix = "" unless defined $pic_prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 2 | $picture =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 2 | return $picture; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | # Split each portion of number and picture into arrays of characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 13 | my @num_int = split(//, $num_int); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 13 | my @num_dec = split(//, $num_dec); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 13 | my @pic_int = split(//, $pic_int); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 13 | my @pic_dec = split(//, $pic_dec); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | # Now we copy those characters into @result. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 13 | my @result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | @result = ($self->{decimal_point}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 13 | ▶ | if $picture =~ /\Q$self->{decimal_point}\E/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | # For each characture in the decimal part of the picture, replace '#' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | # signs with digits from the number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 13 | my $char; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 13 | foreach $char (@pic_dec) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 18 | ▶ | $char = (shift(@num_dec) || 0) if ($char eq '#'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift @num_dec || 0
MC/DC100%1/1
shift @num_dec || 0
shift @num_dec
Truth table100%2/2
A
A= shift @num_dec
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 18 | push (@result, $char); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | # For each character in the integer part of the picture (moving right | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | # to left this time), replace '#' signs with digits from the number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | # or spaces if we've run out of numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 13 | ▶ | while ($char = pop @pic_int) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 143 | ▶ | $char = pop(@num_int) if ($char eq '#'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | $char = ' ' if (!defined($char) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 143 | ▶ | $char eq $self->{thousands_sep} && $#num_int < 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $char or $char eq $self->{"thousands_sep"} and $#num_int < 0
$char eq $self->{"thousands_sep"} and $#num_int < 0
MC/DC100%3/3
not defined $char or $char eq $self->{"thousands_sep"} and $#num_int < 0
not defined $char
$char eq $self->{"thousands_sep"}
$#num_int < 0
Truth table100%4/4
not(A) or B and C
A= not defined $charB= $char eq $self->{"thousands_sep"}C= $#num_int < 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 143 | unshift (@result, $char); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | # Combine @result into a string and return it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 13 | my $result = join('', @result); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 13 | ▶ | $sign_prefix = '' unless defined $sign_prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | 13 | ▶ | $pic_prefix = '' unless defined $pic_prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 13 | $result =~ s/^(\Q$sign_prefix\E)(\Q$pic_prefix\E)(\s*)/$2$3$1/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 13 | $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | #pod =method format_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | #pod my $str = $formatter->format_price($number, $precision, $symbol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | #pod Returns a string containing C<$number> formatted similarly to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | #pod C<format_number()>, except that the decimal portion may have trailing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | #pod zeroes added to make it be exactly C<$precision> characters long, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | #pod the currency string will be prefixed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | #pod The C<$symbol> attribute may be one of "INT_CURR_SYMBOL" or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | #pod "CURRENCY_SYMBOL" (case insensitive) to use the value of that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | #pod attribute of the object, or a string containing the symbol to be used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | #pod The default is "INT_CURR_SYMBOL" if this argument is undefined or not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | #pod given; if set to the empty string, or if set to undef and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | #pod C<INT_CURR_SYMBOL> attribute of the object is the empty string, no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | #pod currency will be added. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | #pod Negative prices are laid out according to the locale's monetary sign | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | #pod rules: C<NEGATIVE_SIGN>, C<N_SIGN_POSN>, C<N_CS_PRECEDES>, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | #pod C<N_SEP_BY_SPACE>. Unlike C<format_number()>, C<format_price()> does | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | #pod I<not> consult C<NEG_FORMAT>. To show negative prices in parentheses, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | #pod for example, set C<N_SIGN_POSN> to 0 rather than setting C<NEG_FORMAT> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | #pod to C<(x)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | #pod If C<$precision> is not provided, the default of 2 will be used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | #pod Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | #pod format_price(12.95) yields 'USD 12.95' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | #pod format_price(12) yields 'USD 12.00' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | #pod format_price(12, 3) yields '12.000' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | #pod The third example assumes that C<INT_CURR_SYMBOL> is the empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | sub format_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | 55 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_price: called CC=28 SCAR=33.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 55 | my ($self, $number, $precision, $curr_symbol) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | 55 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | 1 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | # Determine what the monetary symbol should be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | $curr_symbol = $self->{int_curr_symbol} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | 55 | ▶ | if (!defined($curr_symbol) || lc($curr_symbol) eq "int_curr_symbol"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $curr_symbol or lc $curr_symbol eq "int_curr_symbol"
MC/DC50%1/2
not defined $curr_symbol or lc $curr_symbol eq "int_curr_symbol"
not defined $curr_symbol
lc $curr_symbol eq "int_curr_symbol"
Truth table66%2/3
not(A) or B
A= not defined $curr_symbolB= lc $curr_symbol eq "int_curr_symbol"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | $curr_symbol = $self->{currency_symbol} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | 55 | ▶ | if (!defined($curr_symbol) || lc($curr_symbol) eq "currency_symbol"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $curr_symbol or lc $curr_symbol eq "currency_symbol"
MC/DC50%1/2
not defined $curr_symbol or lc $curr_symbol eq "currency_symbol"
not defined $curr_symbol
lc $curr_symbol eq "currency_symbol"
Truth table66%2/3
not(A) or B
A= not defined $curr_symbolB= lc $curr_symbol eq "currency_symbol"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | 55 | ▶ | $curr_symbol = "" unless defined($curr_symbol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | # Determine which value to use for frac digits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | my $frac_digits = ($curr_symbol eq $self->{int_curr_symbol} ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 55 | ▶ | $self->{int_frac_digits} : $self->{frac_digits}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | # Determine precision for decimal portion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 55 | ▶ | $precision = $frac_digits unless defined $precision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 55 | ▶ | $precision = $self->{decimal_digits} unless defined $precision; # fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 55 | ▶ | $precision = 2 unless defined $precision; # default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | # Determine sign and absolute value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 55 | my $sign = $number <=> 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 55 | ▶ | $number = abs($number) if $sign < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | # format it first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 55 | $number = $self->format_number($number, $precision, undef, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | # Now we make sure the decimal part has enough zeroes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 55 | my ($integer, $decimal) = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | split(/\Q$self->{mon_decimal_point}\E/, $number, 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | 55 | ▶ | $decimal = '0'x$precision unless $decimal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 55 | $decimal .= '0'x($precision - length $decimal); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | # Extract positive or negative values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | 55 | my($sep_by_space, $cs_precedes, $sign_posn, $sign_symbol); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 55 | ▶ | if ($sign < 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | 41 | $sep_by_space = $self->{n_sep_by_space}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | 41 | $cs_precedes = $self->{n_cs_precedes}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 41 | $sign_posn = $self->{n_sign_posn}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | 41 | $sign_symbol = $self->{negative_sign}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | 14 | $sep_by_space = $self->{p_sep_by_space}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 14 | $cs_precedes = $self->{p_cs_precedes}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 14 | $sign_posn = $self->{p_sign_posn}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | 14 | $sign_symbol = $self->{positive_sign}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | # Combine it all back together. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | my $result = ($precision ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 55 | ▶ | join($self->{mon_decimal_point}, $integer, $decimal) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | $integer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | # Determine where spaces go, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | 55 | my($sign_sep, $curr_sep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 55 | ▶ | if ($sep_by_space == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 20 | $sign_sep = $curr_sep = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | elsif ($sep_by_space == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | 23 | $sign_sep = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | 23 | $curr_sep = " "; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | elsif ($sep_by_space == 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | 12 | $sign_sep = " "; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 12 | $curr_sep = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 0 | croak "Invalid sep_by_space value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | # Add sign, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 55 | ▶ | if ($sign_posn >= 0 && $sign_posn <= 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$sign_posn >= 0 and $sign_posn <= 2
MC/DC50%1/2
$sign_posn >= 0 and $sign_posn <= 2
$sign_posn >= 0
$sign_posn <= 2
Truth table66%2/3
A and B
A= $sign_posn >= 0B= $sign_posn <= 2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | # Combine with currency symbol and return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 35 | ▶ | if ($curr_symbol ne "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 35 | ▶ | if ($cs_precedes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | 26 | $result = $curr_symbol.$curr_sep.$result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 9 | $result = $result.$curr_sep.$curr_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | 35 | ▶ | if ($sign_posn == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | 7 | return "($result)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | elsif ($sign_posn == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | 21 | return $sign_symbol.$sign_sep.$result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | else # $sign_posn == 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | 7 | return $result.$sign_sep.$sign_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | ▶ | elsif ($sign_posn == 3 || $sign_posn == 4) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$sign_posn == 3 or $sign_posn == 4
MC/DC0%0/2
$sign_posn == 3 or $sign_posn == 4
$sign_posn == 3
$sign_posn == 4
Truth table66%2/3
A or B
A= $sign_posn == 3B= $sign_posn == 4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | 20 | ▶ | if ($sign_posn == 3) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | 13 | $curr_symbol = $sign_symbol.$sign_sep.$curr_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | else # $sign_posn == 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | 7 | $curr_symbol = $curr_symbol.$sign_sep.$sign_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | # Combine with currency symbol and return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 20 | ▶ | if ($cs_precedes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | 11 | return $curr_symbol.$curr_sep.$result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 9 | return $result.$curr_sep.$curr_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 0 | croak "Invalid *_sign_posn value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | #pod =method format_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | #pod my $str = $formatter->format_bytes($number, %options); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | #pod my $str = $formatter->format_bytes($number, $precision); # deprecated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | #pod Returns a string containing C<$number> formatted similarly to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | #pod C<format_number()>, except that large numbers may be abbreviated by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | #pod adding a suffix to indicate 1024, 1,048,576, or 1,073,741,824 bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | #pod Suffix may be the traditional K, M, or G (default); or the IEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | #pod standard 60027 "KiB," "MiB," or "GiB" depending on the "mode" option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | #pod Negative values will result in an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | #pod The abbreviated value is passed through C<format_number()>, so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | #pod object's C<THOUSANDS_SEP>, C<DECIMAL_POINT>, and C<DECIMAL_FILL> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | #pod settings all apply, and there is no per-call option to override them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | #pod In particular, to get trailing zeroes (for aligning a column of sizes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | #pod say) build the formatter with C<< decimal_fill => 1 >>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | #pod Number::Format->new->format_bytes(2048) yields 2K | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | #pod Number::Format->new(decimal_fill => 1)->format_bytes(2048) yields 2.00K | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | #pod The second parameter can be either a hash that sets options, or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | #pod number. Using a number here is deprecated and will generate a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | #pod warning; early versions of Number::Format only allowed a numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | #pod value. A future release of Number::Format will change this warning to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | #pod an error. New code should use a hash instead to set options. If it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | #pod is a number this sets the value of the "precision" option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | #pod Valid options are: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | #pod =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | #pod =item precision | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | #pod Set the precision for displaying numbers. If not provided, a default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | #pod of 2 will be used. Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | #pod format_bytes(12.95) yields '12.95' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | #pod format_bytes(12.95, precision => 0) yields '13' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | #pod format_bytes(2048) yields '2K' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | #pod format_bytes(2048, mode => "iec") yields '2KiB' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | #pod format_bytes(9999999) yields '9.54M' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | #pod format_bytes(9999999, precision => 1) yields '9.5M' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | #pod =item unit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | #pod Sets the default units used for the results. The default is to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | #pod determine this automatically in order to minimize the length of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | #pod string. In other words, numbers greater than or equal to 1024 (or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | #pod other number given by the 'base' option, q.v.) will be divided by 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | #pod and C<$KILO_SUFFIX> or C<$KIBI_SUFFIX> added; if greater than or equal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | #pod to 1048576 (1024*1024), it will be divided by 1048576 and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | #pod C<$MEGA_SUFFIX> or C<$MEBI_SUFFIX> appended to the end; etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | #pod However if a value is given for C<unit> it will use that value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | #pod instead. The first letter (case-insensitive) of the value given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | #pod indicates the threshhold for conversion; acceptable values are T (for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | #pod tera/tebi), G (for giga/gibi), M (for mega/mebi), K (for kilo/kibi), or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | #pod A (for automatic, the default). For example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | #pod format_bytes(1048576, unit => 'K') yields '1,024K' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | #pod instead of '1M' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | #pod Note that the valid values to this option do not vary even when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | #pod suffix configuration variables have been changed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | #pod =item base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | #pod Sets the number at which the C<$KILO_SUFFIX> is added. Default is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | #pod 1024. Set to any value; the only other useful value is probably 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | #pod as hard disk manufacturers use that number to make their disks sound | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | #pod bigger than they really are. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | #pod If the mode (see below) is set to "iec" or "iec60027" then setting the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | #pod base option results in an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | #pod =item mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | #pod Traditionally, bytes have been given in SI (metric) units such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | #pod "kilo" and "mega" even though they represent powers of 2 (1024, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | #pod rather than powers of 10 (1000, etc.) This "binary prefix" causes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | #pod much confusion in consumer products where "GB" may mean either | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | #pod 1,048,576 or 1,000,000, for example. The International | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | #pod Electrotechnical Commission has created standard IEC 60027 to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | #pod introduce prefixes Ki, Mi, Gi, etc. ("kibibytes," "mebibytes," | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | #pod "gibibytes," etc.) to remove this confusion. Specify a mode option | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | #pod with either "traditional" or "iec60027" (or abbreviate as "trad" or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | #pod "iec") to indicate which type of binary prefix you want format_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | #pod to use. For backward compatibility, "traditional" is the default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | #pod See http://en.wikipedia.org/wiki/Binary_prefix for more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | sub format_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | 22 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_bytes: called CC=21 SCAR=30.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | 22 | my ($self, $number, @options) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | 22 | ▶ | unless (defined($number)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | 1 | $number = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | 22 | ▶ | croak "Negative number not allowed in format_bytes" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | if $number < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | # If a single scalar is given instead of key/value pairs for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | # @options, treat that as the value of the precision option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | 22 | my %options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | 22 | ▶ | if (@options == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # To be changed to 'croak' in a future release: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 0 | carp "format_bytes: number instead of options is deprecated"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 0 | %options = ( precision => $options[0] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | 22 | %options = @options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | # Set default for precision. Test using defined because it may be 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | $options{precision} = $self->{decimal_digits} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | 22 | ▶ | unless defined $options{precision}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | $options{precision} = 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 22 | ▶ | unless defined $options{precision}; # default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | 22 | ▶ | $options{mode} ||= "traditional"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"mode"} ||= "traditional"
MC/DC100%1/1
$options{"mode"} ||= "traditional"
$options{"mode"}
Truth table100%2/2
A
A= $options{"mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | 22 | my($ksuff, $msuff, $gsuff, $tsuff); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | 22 | ▶ | if ($options{mode} =~ /^iec(60027)?$/i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | ($ksuff, $msuff, $gsuff, $tsuff) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | 8 | @$self{qw(kibi_suffix mebi_suffix gibi_suffix tebi_suffix)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | croak "base option not allowed in iec60027 mode" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | 8 | ▶ | if exists $options{base}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | elsif ($options{mode} =~ /^trad(itional)?$/i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | ($ksuff, $msuff, $gsuff, $tsuff) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | 14 | @$self{qw(kilo_suffix mega_suffix giga_suffix tera_suffix)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | 0 | croak "Invalid mode"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # Set default for "base" option. Calculate threshold values for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | # kilo, mega, giga, and tera values. Useful values of "base" are 1024 or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | # 1000, but any number can be used. Larger numbers may cause overflows at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | # some threshholds, of course. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | 22 | my %mult = _get_multipliers($options{base}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | # Process "unit" option. Set default, then take first character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | # and convert to upper case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | $options{unit} = "auto" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | 22 | ▶ | unless defined $options{unit}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | 22 | my $unit = uc(substr($options{unit},0,1)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | # Process "auto" first (default). Based on size of number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | # automatically determine which unit to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | 22 | ▶ | if ($unit eq 'A') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | 19 | ▶ | if ($number >= $mult{tera}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 3 | $unit = 'T'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | elsif ($number >= $mult{giga}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | 2 | $unit = 'G'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | elsif ($number >= $mult{mega}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 6 | $unit = 'M'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | elsif ($number >= $mult{kilo}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | 3 | $unit = 'K'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | 5 | $unit = 'N'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | # Based on unit, whether specified or determined above, divide the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | # number and determine what suffix to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | 22 | my $suffix = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | 22 | ▶ | if ($unit eq 'T') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | 4 | $number /= $mult{tera}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | 4 | $suffix = $tsuff; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | elsif ($unit eq 'G') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | 2 | $number /= $mult{giga}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | 2 | $suffix = $gsuff; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | elsif ($unit eq 'M') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 6 | $number /= $mult{mega}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | 6 | $suffix = $msuff; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | elsif ($unit eq 'K') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | 5 | $number /= $mult{kilo}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | 5 | $suffix = $ksuff; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | elsif ($unit ne 'N') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | 0 | croak "Invalid unit option"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | # Format the number and add the suffix. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | 22 | return $self->format_number($number, $options{precision}) . $suffix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | #pod =method unformat_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | #pod my $str = $formatter->format_number($formatted); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | #pod Converts a string as returned by C<format_number()>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | #pod C<format_price()>, or C<format_picture()>, and returns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | #pod corresponding value as a numeric scalar. Returns C<undef> if the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | #pod number does not contain any digits. Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | #pod unformat_number('USD 12.95') yields 12.95 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | #pod unformat_number('USD 12.00') yields 12 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | #pod unformat_number('foobar') yields undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | #pod unformat_number('1234-567@.8') yields 1234567.8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | #pod The value of C<DECIMAL_POINT> is used to determine where to separate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | #pod the integer and decimal portions of the input. All other non-digit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | #pod characters, including but not limited to C<INT_CURR_SYMBOL> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | #pod C<THOUSANDS_SEP>, are removed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | #pod If the number matches the pattern of C<NEG_FORMAT> I<or> there is a C<-> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | #pod character before any of the digits, then a negative number is returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | #pod If the number ends with the C<KILO_SUFFIX>, C<KIBI_SUFFIX>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | #pod C<MEGA_SUFFIX>, C<MEBI_SUFFIX>, C<GIGA_SUFFIX>, C<GIBI_SUFFIX>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | #pod C<TERA_SUFFIX>, or C<TEBI_SUFFIX> characters, then the number returned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | #pod will be multiplied by the appropriate multiple of 1024 (or if the base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | #pod option is given, by the multiple of that value) as appropriate. Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | #pod unformat_number("4K", base => 1024) yields 4096 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | #pod unformat_number("4K", base => 1000) yields 4000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | #pod unformat_number("4KiB", base => 1024) yields 4096 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | #pod unformat_number("4G") yields 4294967296 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | sub unformat_number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | 23 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unformat_number: called CC=17 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | 23 | my ($self, $formatted, %options) = _get_self @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | 23 | ▶ | unless (defined($formatted)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 1 | _complain_undef(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 1 | $formatted = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | 23 | $self->_check_seps(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | 23 | ▶ | return undef unless $formatted =~ /\d/; # require at least one digit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | # Regular expression for detecting decimal point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | 21 | my $pt = qr/\Q$self->{decimal_point}\E/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | # ru_RU locale has comma for decimal_point, but period for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | # mon_decimal_point! But as long as thousands_sep is different | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | # from either, we can allow either decimal point. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | 21 | ▶ | if ($self->{mon_decimal_point} && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_thousands_sep"} and $self->{"mon_decimal_point"} ne $self->{"thousands_sep"}
$self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_thousands_sep"}
$self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_decimal_point"}
MC/DC0%0/4
$self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_decimal_point"} and $self->{"decimal_point"} ne $self->{"mon_thousands_sep"} and $self->{"mon_decimal_point"} ne $self->{"thousands_sep"}
$self->{"mon_decimal_point"}
$self->{"decimal_point"} ne $self->{"mon_decimal_point"}
$self->{"decimal_point"} ne $self->{"mon_thousands_sep"}
$self->{"mon_decimal_point"} ne $self->{"thousands_sep"}
Truth table20%1/5
A and B and C and D
A= $self->{"mon_decimal_point"}B= $self->{"decimal_point"} ne $self->{"mon_decimal_point"}C= $self->{"decimal_point"} ne $self->{"mon_thousands_sep"}D= $self->{"mon_decimal_point"} ne $self->{"thousands_sep"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | $self->{decimal_point} ne $self->{mon_decimal_point} && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | $self->{decimal_point} ne $self->{mon_thousands_sep} && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | $self->{mon_decimal_point} ne $self->{thousands_sep}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | 0 | $pt = qr/(?:\Q$self->{decimal_point}\E| | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | \Q$self->{mon_decimal_point}\E)/x; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | # Detect if it ends with one of the kilo / mega / giga / tera suffixes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | 21 | my $kp = ($formatted =~ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | s/\s*($self->{kilo_suffix}|$self->{kibi_suffix})\s*$//); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | 21 | my $mp = ($formatted =~ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | s/\s*($self->{mega_suffix}|$self->{mebi_suffix})\s*$//); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | 21 | my $gp = ($formatted =~ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | s/\s*($self->{giga_suffix}|$self->{gibi_suffix})\s*$//); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | 21 | my $tp = ($formatted =~ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | s/\s*($self->{tera_suffix}|$self->{tebi_suffix})\s*$//); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | 21 | my %mult = _get_multipliers($options{base}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | # Split number into integer and decimal parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 17 | my ($integer, $decimal, @cruft) = split($pt, $formatted); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | 17 | ▶ | croak "Only one decimal separator permitted" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | if @cruft; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | # It's negative if the first non-digit character is a - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | 17 | ▶ | my $sign = $formatted =~ /^\D*-/ ? -1 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | 17 | my($before_re, $after_re) = split /x/, $self->{neg_format}, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | 17 | ▶ | $sign = -1 if $formatted =~ /\Q$before_re\E(.+)\Q$after_re\E/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | # Strip out all non-digits from integer and decimal parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | 17 | ▶ | $integer = '' unless defined $integer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | 17 | ▶ | $decimal = '' unless defined $decimal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | 17 | $integer =~ s/\D//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | 17 | $decimal =~ s/\D//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | # Join back up, using period, and add 0 to make Perl think it's a number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | 17 | my $number = join('.', $integer, $decimal) + 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | 17 | ▶ | $number = -$number if $sign < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | # Scale the number if it ended in kilo or mega suffix. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | 17 | ▶ | $number *= $mult{kilo} if $kp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | 17 | ▶ | $number *= $mult{mega} if $mp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 17 | ▶ | $number *= $mult{giga} if $gp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 17 | ▶ | $number *= $mult{tera} if $tp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | 17 | return $number; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | ###--------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | #pod =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | #pod Some systems, notably OpenBSD, may have incomplete locale support. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | #pod Using this module together with L<setlocale(3)> in OpenBSD may therefore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | #pod not produce the intended results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | #pod This library takes some pain to cope with numeric precision, but it doesn't | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | #pod solve every problem. Working with terabyte sizes on a 32-bit perl may have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | #pod imprecise results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||