⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.pm

📁 Astercon2 开源软交换 2.2.0
💻 PM
📖 第 1 页 / 共 3 页
字号:
package Number::Format;# Due to differences in the locale system Perl 5.8 is required for# this module to function properly.  You may be able to get it to work# on older Perls by uncommenting this line, but it is not supported.# I recommend you either upgrade Perl or download an older version of# this module in that case.require 5.008;=head1 NAMENumber::Format - Perl extension for formatting numbers=head1 SYNOPSIS  use Number::Format;  my $x = new Number::Format %args;  $formatted = $x->round($number, $precision);  $formatted = $x->format_number($number, $precision, $trailing_zeroes);  $formatted = $x->format_negative($number, $picture);  $formatted = $x->format_picture($number, $picture);  $formatted = $x->format_price($number, $precision);  $formatted = $x->format_bytes($number, $precision);  $number    = $x->unformat_number($formatted);  use Number::Format qw(:subs);  $formatted = round($number, $precision);  $formatted = format_number($number, $precision, $trailing_zeroes);  $formatted = format_negative($number, $picture);  $formatted = format_picture($number, $picture);  $formatted = format_price($number, $precision);  $formatted = format_bytes($number, $precision);  $number    = unformat_number($formatted);=head1 REQUIRESPerl, version 5.8 or higher.POSIX.pm to determine locale settings.Carp.pm is used for some error reporting.=head1 DESCRIPTIONThese functions provide an easy means of formatting numbers in amanner suitable for displaying to the user.There are two ways to use this package.  One is to declare an objectof type Number::Format, which you can think of as a formatting engine.The various functions defined here are provided as object methods.The constructor C<new()> can be used to set the parameters of theformatting engine.  Valid parameters are:  THOUSANDS_SEP     - character inserted between groups of 3 digits  DECIMAL_POINT     - character separating integer and fractional parts  MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price  MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price  INT_CURR_SYMBOL   - character(s) denoting currency (see format_price())  DECIMAL_DIGITS    - number of digits to the right of dec point (def 2)  DECIMAL_FILL      - boolean; whether to add zeroes to fill out decimal  NEG_FORMAT        - format to display negative numbers (def ``-x'')  KILO_SUFFIX       - suffix to add when format_bytes formats kilobytes  MEGA_SUFFIX       -    "    "  "    "        "         "    megabytes  GIGA_SUFFIX       -    "    "  "    "        "         "    gigabytesThey may be specified in upper or lower case, with or without aleading hyphen ( - ).If C<THOUSANDS_SEP> is set to the empty string, format_number will notinsert any separators.The defaults for C<THOUSANDS_SEP>, C<DECIMAL_POINT>,C<MON_THOUSANDS_SEP>, C<MON_DECIMAL_POINT>, and C<INT_CURR_SYMBOL>come from the POSIX locale information (see L<perllocale>).  If yourPOSIX locale does not provide C<MON_THOUSANDS_SEP> and/orC<MON_DECIMAL_POINT> fields, then the C<THOUSANDS_SEP> and/orC<DECIMAL_POINT> values are used for those parameters.  Formerly,POSIX was optional but this caused problems in some cases, so it isnow required.  If this causes you hardship, please contact the authorof this package at <SwPrAwM@cpan.org> (remove "SPAM" to get correctemail address) for help.If any of the above parameters are not specified when you invokeC<new()>, then the values are taken from package global variables ofthe same name (e.g.  C<$DECIMAL_POINT> is the default for theC<DECIMAL_POINT> parameter).  If you use the C<:vars> keyword on yourC<use Number::Format> line (see non-object-oriented example below) youwill import those variables into your namesapce and can assign valuesas if they were your own local variables.  The default values for allthe parameters are:  THOUSANDS_SEP     = ','  DECIMAL_POINT     = '.'  MON_THOUSANDS_SEP = ','  MON_DECIMAL_POINT = '.'  INT_CURR_SYMBOL   = 'USD'  DECIMAL_DIGITS    = 2  DECIMAL_FILL      = 0  NEG_FORMAT        = '-x'  KILO_SUFFIX       = 'K'  MEGA_SUFFIX       = 'M'  GIGA_SUFFIX       = 'G'Note however that when you first call one of the functions in thismodule I<without> using the object-oriented interface, further settingof those global variables will have no effect on non-OO calls.  It isrecommended that you use the object-oriented interface instead forfewer headaches and a cleaner design.The C<DECIMAL_FILL> and C<DECIMAL_DIGITS> values are not set by theLocale system, but are definable by the user.  They affect the outputof C<format_number()>.  Setting C<DECIMAL_DIGITS> is like giving thatvalue as the C<$precision> argument to that function.  SettingC<DECIMAL_FILL> to a true value causes C<format_number()> to appendzeroes to the right of the decimal digits until the length is thespecified number of digits.C<NEG_FORMAT> is only used by C<format_negative()> and is a stringcontaining the letter 'x', where that letter will be replaced by apositive representation of the number being passed to that function.C<format_number()> and C<format_price()> utilize this feature bycalling C<format_negative()> if the number was less than 0.C<KILO_SUFFIX>, C<MEGA_SUFFIX>, and C<GIGA_SUFFIX> are used byC<format_bytes()> when the value is over 1024, 1024*1024, or1024*1024*1024, respectively.  The default values are "K", "M", and"G".  Note: we can not do TERA because of integer overflows on 32-bitsystems.The only restrictions on C<DECIMAL_POINT> and C<THOUSANDS_SEP> are thatthey must not be digits, must not be identical, and must each be onecharacter.  There are no restrictions on C<INT_CURR_SYMBOL>.For example, a German user might include this in their code:  use Number::Format;  my $de = new Number::Format(-thousands_sep   => '.',                              -decimal_point   => ',',                              -int_curr_symbol => 'DEM');  my $formatted = $de->format_number($number);Or, if you prefer not to use the object oriented interface, you can dothis instead:  use Number::Format qw(:subs :vars);  $THOUSANDS_SEP   = '.';  $DECIMAL_POINT   = ',';  $INT_CURR_SYMBOL = 'DEM';  my $formatted = format_number($number);=head1 EXPORTSNothing is exported by default.  To export the functions or the globalvariables defined herein, specify the function name(s) on the importlist of the C<use Number::Format> statement.  To export all functionsdefined herein, use the special tag C<:subs>.  To export thevariables, use the special tag C<:vars>; to export both subs and varsyou can use the tag C<:all>.=cut###---------------------------------------------------------------------use strict;use Exporter;use Carp;use POSIX;our @ISA     = qw(Exporter);our @EXPORT_SUBS = qw(format_number format_negative format_picture                      format_price format_bytes round unformat_number);our @EXPORT_VARS = qw($DECIMAL_DIGITS $DECIMAL_FILL $DECIMAL_POINT                      $DEFAULT_LOCALE $INT_CURR_SYMBOL $KILO_SUFFIX                      $MEGA_SUFFIX $GIGA_SUFFIX $NEG_FORMAT $THOUSANDS_SEP);our @EXPORT_OK   = (@EXPORT_SUBS, @EXPORT_VARS);our %EXPORT_TAGS = (subs => \@EXPORT_SUBS,                    vars => \@EXPORT_VARS,                    all  => [ @EXPORT_SUBS, @EXPORT_VARS ]);our $VERSION = '1.52';our $DECIMAL_POINT   = '.';our $THOUSANDS_SEP   = ',';our $INT_CURR_SYMBOL = 'USD';our $DECIMAL_DIGITS  = 2;our $DECIMAL_FILL    = 0;our $NEG_FORMAT      = '-x';our $KILO_SUFFIX     = 'K';our $MEGA_SUFFIX     = 'M';our $GIGA_SUFFIX     = 'G';our $DEFAULT_LOCALE = { (mon_thousands_sep => $THOUSANDS_SEP,                         mon_decimal_point => $DECIMAL_POINT,                         thousands_sep     => $THOUSANDS_SEP,                         decimal_point     => $DECIMAL_POINT,                         int_curr_symbol   => $INT_CURR_SYMBOL,                         neg_format        => $NEG_FORMAT,                         kilo_suffix       => $KILO_SUFFIX,                         mega_suffix       => $MEGA_SUFFIX,                         giga_suffix       => $GIGA_SUFFIX,                         decimal_digits    => $DECIMAL_DIGITS,                         decimal_fill      => $DECIMAL_FILL,                        ) };###---------------------------------------------------------------------# INTERNAL FUNCTIONS# These functions (with names beginning with '_' are for internal use# only.  There is no guarantee that they will remain the same from one# version to the next!##----------------------------------------------------------------------# _get_self creates an instance of Number::Format with the default#     values for the configuration parameters, if the first element of#     @_ is not already an object.my $DefaultObject;sub _get_self{    unless (ref $_[0])    {        $DefaultObject ||= new Number::Format();        unshift (@_, $DefaultObject);    }    @_;}##----------------------------------------------------------------------# _check_seps is used to validate that the thousands_sep and#     decimal_point variables have acceptable values.  For internal use#     only.sub _check_seps{    my ($self) = @_;    croak "Not an object" unless ref $self;    croak "Number::Format: {thousands_sep} is undefined\n"        unless defined $self->{thousands_sep};    croak "Number::Format: {thousands_sep} is too long (max 1 character)\n"        if length $self->{thousands_sep} > 1;    croak "Number::Format: {thousands_sep} may not be numeric\n"        if $self->{thousands_sep} =~ /\d/;    croak "Number::Format: {decimal_point} must be one character\n"        if length $self->{decimal_point} != 1;    croak "Number::Format: {decimal_point} may not be numeric\n"        if $self->{decimal_point} =~ /\d/;    croak("Number::Format: {thousands_sep} and {decimal_point} ".          "may not be equal\n")        if $self->{decimal_point} eq $self->{thousands_sep};}###---------------------------------------------------------------------=head1 METHODS=over 4=cut##----------------------------------------------------------------------=item new( %args )Creates a new Number::Format object.  Valid keys for %args are any ofthe parameters described above.  Keys may be in all uppercase or alllowercase, and may optionally be preceded by a hyphen (-) character.Example:  my $de = new Number::Format(-thousands_sep   => '.',                              -decimal_point   => ',',                              -int_curr_symbol => 'DEM');=cutsub new{    my $type = shift;    my %args = @_;    # Fetch defaults from current locale, or failing that, using globals    my $me            = {};    my $locale        = setlocale(LC_ALL);    my $locale_values = localeconv();    my $arg;    foreach $arg (keys %$locale_values)    {        $me->{$arg} = $locale_values->{$arg};    }    $me->{mon_decimal_point} ||= $DECIMAL_POINT;    $me->{mon_thousands_sep} ||= $THOUSANDS_SEP;    $me->{int_curr_symbol}   ||= $INT_CURR_SYMBOL;    $me->{decimal_digits}    ||= $DECIMAL_DIGITS;    $me->{decimal_fill}      ||= $DECIMAL_FILL;    $me->{neg_format}        ||= $NEG_FORMAT;    $me->{kilo_suffix}       ||= $KILO_SUFFIX;    $me->{mega_suffix}       ||= $MEGA_SUFFIX;    $me->{giga_suffix}       ||= $GIGA_SUFFIX;    $me->{thousands_sep}     ||= $me->{mon_thousands_sep};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -