The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::API::Stripe::Number::Format - Number Formatting

SYNOPSIS

    use Net::API::Stripe::Number::Format;
    my $x = Net::API::Stripe::Number::Format->new( %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, $symbol );
    $formatted = $x->format_bytes( $number, $precision );
    $number    = $x->unformat_number( $formatted );

VERSION

    0.2

DESCRIPTION

These functions provide an easy means of formatting numbers in a manner suitable for displaying to the user.

You can declare an object of type Number::Format, which you can think of as a formatting engine. The various functions defined here are provided as object methods. The constructor new() can be used to set the parameters of the formatting 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 (trad)

mega_suffix

suffix to add when format_bytes formats megabytes (trad)

giga_suffix

suffix to add when format_bytes formats gigabytes (trad)

kibi_suffix

suffix to add when format_bytes formats kibibytes (iec)

mebi_suffix

suffix to add when format_bytes formats mebibytes (iec)

gibi_suffix

suffix to add when format_bytes formats gibibytes (iec)

They may be specified in upper or lower case, with or without a leading hyphen ( - ).

If thousands_sep is set to the empty string, format_number will not insert any separators.

The defaults for thousands_sep, decimal_point, mon_thousands_sep, mon_decimal_point, and int_curr_symbol come from the POSIX locale information (see perllocale). If your POSIX locale does not provide mon_thousands_sep and/or mon_decimal_point fields, then the thousands_sep and/or decimal_point values are used for those parameters.

The default object values for all the 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'
  kibi_suffix       = 'KiB'
  mebi_suffix       = 'MiB'
  gibi_suffix       = 'GiB'

The decimal_fill and decimal_digits values are not set by the Locale system, but are definable by the user. They affect the output of "format_number". Setting decimal_digits is like giving that value as the $precision argument to that function. Setting decimal_fill to a true value causes "format_number" to append zeroes to the right of the decimal digits until the length is the specified number of digits.

neg_format is only used by "format_negative" and is a string containing the letter 'x', where that letter will be replaced by a positive representation of the number being passed to that function. "format_number" and "format_price" utilise this feature by calling "format_negative" if the number was less than 0.

kilo_suffix, mega_suffix, and giga_suffix are used by "format_bytes" when the value is over 1024, 1024*1024, or 1024*1024*1024, respectively. The default values are "K", "M", and "G". These apply in the default "traditional" mode only. Note: TERA or higher are not implemented because of integer overflows on 32-bit systems.

kibi_suffix, mebi_suffix, and gibi_suffix are used by "format_bytes" when the value is over 1024, 1024*1024, or 1024*1024*1024, respectively. The default values are "KiB", "MiB", and "GiB". These apply in the "iso60027"" mode only. Note: TEBI or higher are not implemented because of integer overflows on 32-bit systems.

The only restrictions on decimal_point and thousands_sep are that they must not be digits and must not be identical. There are no restrictions on int_curr_symbol.

For example, a German user might include this in their code:

  use Net::API::Stripe::Number::Format;
  my $de = new Net::API::Stripe::Number::Format(
      thousands_sep   => '.',
      decimal_point   => ',',
      int_curr_symbol => 'DEM'
  );
  my $formatted = $de->format_number( $number );

REQUIRES

Perl, version 5.8 or higher.

POSIX to determine locale settings.

ERROR HANDLING

This module never dies, or at least not voluntarily. It does not use carp, or croak.

When an error occurs, it returns undef, or an empty list depending on the context, after having set an error which can be retrieved with the "error" in Module::Generic this module inherits

METHODS

new

Provided a hash or hash reference of parameters, this creates a new Net::API::Stripe::Number::Format object. Valid keys for %args are any of the parameters described above. Keys must be in lowercase. Example:

  my $de = new Net::API::Stripe::Number::Format(
      thousands_sep   => '.',
      decimal_point   => ',',
      int_curr_symbol => 'DEM'
  );
round

This takes 2 parameters: $number and $precision

Rounds the number to the specified precision. If $precision is omitted, the value of the decimal_digits parameter is used (default value 2). Both input and output are numeric (the function uses math operators rather than string manipulation to do its job), The value of $precision may be any integer, positive or negative. Examples:

  round( 3.14159 )       yields    3.14
  round( 3.14159, 4 )    yields    3.1416
  round( 42.00, 4 )      yields    42
  round( 1234, -2 )      yields    1200

Since this is a mathematical rather than string oriented function, there will be no trailing zeroes to the right of the decimal point, and the decimal_point and thousands_sep variables are ignored. To format your number using the decimal_point and thousands_sep variables, use "format_number" instead.

format_number

This takes 3 parameters: $number, $precision and trailing_zeroes

Formats a number by adding thousands_sep between each set of 3 digits to the left of the decimal point, substituting decimal_point for the decimal point, and rounding to the specified precision using "round". Note that $precision is a maximum precision specifier; trailing zeroes will only appear in the output if $trailing_zeroes is provided, or the parameter decimal_fill is set, with a value that is true (not zero, undef, or the empty string). If $precision is omitted, the value of the object decimal_digits parameter (default value of 2) is used.

If the value is too large or great to work with as a regular number, but instead must be shown in scientific notation, returns that number in scientific notation without further formatting.

Examples:

  format_number( 12345.6789 )             yields   '12,345.68'
  format_number( 123456.789, 2 )          yields   '123,456.79'
  format_number( 1234567.89, 2 )          yields   '1,234,567.89'
  format_number( 1234567.8, 2 )           yields   '1,234,567.8'
  format_number( 1234567.8, 2, 1 )        yields   '1,234,567.80'
  format_number( 1.23456789, 6 )          yields   '1.234568'
  format_number( "0.000020000E+00", 7 );' yields   '2e-05'

Of course the output would have your values of THOUSANDS_SEP and DECIMAL_POINT instead of ',' and '.' respectively.

format_negative

This takes 2 parameters: $number and $picture

Formats a negative number. Picture should be a string that contains the letter x where the number should be inserted. For example, for standard negative numbers you might use ``-x'', while for accounting purposes you might use ``(x)''. If the specified number begins with a ``-'' character, that will be removed before formatting, but formatting will occur whether or not the number is negative.

format_picture

This takes 2 parameters: $number and $picture

Returns a string based on $picture with the # characters replaced by digits from $number. If the length of the integer part of $number is too large to fit, the # characters are replaced with asterisks (*) instead. Examples:

  format_picture( 100.023, 'USD ##,###.##' )   yields   'USD    100.02'
  format_picture( 1000.23, 'USD ##,###.##' )   yields   'USD  1,000.23'
  format_picture( 10002.3, 'USD ##,###.##' )   yields   'USD 10,002.30'
  format_picture( 100023,  'USD ##,###.##' )   yields   'USD **,***.**'
  format_picture( 1.00023, 'USD #.###,###' )   yields   'USD 1.002,300'

The comma (,) and period (.) you see in the picture examples should match the values of thousands_sep and decimal_point, respectively, for proper operation. However, the thousands_sep characters in $picture need not occur every three digits; the only use of that variable by this function is to remove leading commas (see the first example above). There may not be more than one instance of decimal_point in $picture.

The value of neg_format is used to determine how negative numbers are displayed. The result of this is that the output of this function my have unexpected spaces before and/or after the number. This is necessary so that positive and negative numbers are formatted into a space the same size. If you are only using positive numbers and want to avoid this problem, set neg_format to "x".

format_price( $number, $precision, $symbol )

This takes 3 parameters: $number, $precision and $symbol

Returns a string containing $number formatted similarly to "format_number", except that the decimal portion may have trailing zeroes added to make it be exactly $precision characters long, and the currency string will be prefixed.

The $symbol attribute may be one of int_curr_symbol or currency_symbol (case insensitive) to use the value of that attribute of the object, or a string containing the symbol to be used. The default is int_curr_symbol if this argument is undefined or not given; if set to the empty string, or if set to undef and the int_curr_symbol attribute of the object is the empty string, no currency will be added.

If $precision is not provided, the default of 2 will be used. Examples:

  format_price( 12.95 )   yields   'USD 12.95'
  format_price( 12 )      yields   'USD 12.00'
  format_price( 12, 3 )   yields   '12.000'

The third example assumes that int_curr_symbol is the empty string.

format_bytes( $number, %options )

This takes 2 or more parameters: $number and %options

Returns a string containing $number formatted similarly to "format_number", except that large numbers may be abbreviated by adding a suffix to indicate 1024, 1,048,576, or 1,073,741,824 bytes. Suffix may be the traditional K, M, or G (default); or the IEC standard 60027 "KiB," "MiB," or "GiB" depending on the "mode" option.

Negative values will result in an error.

The second parameter is a hash that sets the following possible options:

precision

Set the precision for displaying numbers. If not provided, a default of 2 will be used. Examples:

  format_bytes( 12.95 )                   yields   '12.95'
  format_bytes( 12.95, precision => 0 )   yields   '13'
  format_bytes( 2048 )                    yields   '2K'
  format_bytes( 2048, mode => "iec" )     yields   '2KiB'
  format_bytes( 9999999 )                 yields   '9.54M'
  format_bytes( 9999999, precision => 1 ) yields   '9.5M'
unit

Sets the default units used for the results. The default is to determine this automatically in order to minimize the length of the string. In other words, numbers greater than or equal to 1024 (or other number given by the 'base' option, q.v.) will be divided by 1024 and kilo_suffix or kibi_suffix added; if greater than or equal to 1048576 (1024*1024), it will be divided by 1048576 and mega_suffix or mebi_suffix appended to the end; etc.

However if a value is given for unit it will use that value instead. The first letter (case-insensitive) of the value given indicates the threshhold for conversion; acceptable values are G (for giga/gibi), M (for mega/mebi), K (for kilo/kibi), or A (for automatic, the default). For example:

  format_bytes( 1048576, unit => 'K' ) yields     '1,024K'
                                       instead of '1M'

Note that the valid values to this option do not vary even when the suffix configuration variables have been changed.

base

Sets the number at which the kilo_suffix is added. Default is 1024. Set to any value; the only other useful value is probably 1000, as hard disk manufacturers use that number to make their disks sound bigger than they really are.

If the mode (see below) is set to "iec" or "iec60027" then setting the base option results in an error.

mode

Traditionally, bytes have been given in SI (metric) units such as "kilo" and "mega" even though they represent powers of 2 (1024, etc.) rather than powers of 10 (1000, etc.) This "binary prefix" causes much confusion in consumer products where "GB" may mean either 1,048,576 or 1,000,000, for example. The International Electrotechnical Commission has created standard IEC 60027 to introduce prefixes Ki, Mi, Gi, etc. ("kibibytes," "mebibytes," "gibibytes," etc.) to remove this confusion. Specify a mode option with either "traditional" or "iec60027" (or abbreviate as "trad" or "iec") to indicate which type of binary prefix you want format_bytes to use. For backward compatibility, "traditional" is the default. See http://en.wikipedia.org/wiki/Binary_prefix for more information.

unformat_number

This takes 1 parameter: $formatted

Converts a string as returned by "format_number", "format_price", or "format_picture", and returns the corresponding value as a numeric scalar. Returns undef if the number does not contain any digits. Examples:

  unformat_number( 'USD 12.95' )   yields   12.95
  unformat_number( 'USD 12.00' )   yields   12
  unformat_number( 'foobar' )      yields   undef
  unformat_number( '1234-567@.8' ) yields   1234567.8

The value of decimal_point is used to determine where to separate the integer and decimal portions of the input. All other non-digit characters, including but not limited to int_curr_symbol and thousands_sep, are removed.

If the number matches the pattern of neg_format or there is a ``-'' character before any of the digits, then a negative number is returned.

If the number ends with the kilo_suffix, kibi_suffix, mega_suffix, mebi_suffix, giga_suffix, or gibi_suffix characters, then the number returned will be multiplied by the appropriate multiple of 1024 (or if the base option is given, by the multiple of that value) as appropriate. Examples:

  unformat_number( "4K", base => 1024 )   yields  4096
  unformat_number( "4K", base => 1000 )   yields  4000
  unformat_number( "4KiB", base => 1024 ) yields  4096
  unformat_number( "4G" )                 yields  4294967296

CAVEATS

Some systems, notably OpenBSD, may have incomplete locale support. Using this module together with setlocale(3) in OpenBSD may therefore not produce the intended results.

CREDITS

Adapted from the original work by William R. Ward

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Net::API::Stripe::Number, Number::Format

COPYRIGHT & LICENSE

Copyright (c) 2019-2020 DEGUEST Pte. Ltd.

You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 870:

You forgot a '=back' before '=head1'