{
    "mode": "perldoc",
    "parameter": "Number::Format",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Number%3A%3AFormat/json",
    "generated": "2026-06-12T09:03:37Z",
    "synopsis": "use Number::Format;\nmy $x = new Number::Format %args;\n$formatted = $x->round($number, $precision);\n$formatted = $x->formatnumber($number, $precision, $trailingzeroes);\n$formatted = $x->formatnegative($number, $picture);\n$formatted = $x->formatpicture($number, $picture);\n$formatted = $x->formatprice($number, $precision, $symbol);\n$formatted = $x->formatbytes($number, $precision);\n$number    = $x->unformatnumber($formatted);\nuse Number::Format qw(:subs);\n$formatted = round($number, $precision);\n$formatted = formatnumber($number, $precision, $trailingzeroes);\n$formatted = formatnegative($number, $picture);\n$formatted = formatpicture($number, $picture);\n$formatted = formatprice($number, $precision, $symbol);\n$formatted = formatbytes($number, $precision);\n$number    = unformatnumber($formatted);",
    "sections": {
        "NAME": {
            "content": "Number::Format - Perl extension for formatting numbers\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Number::Format;\nmy $x = new Number::Format %args;\n$formatted = $x->round($number, $precision);\n$formatted = $x->formatnumber($number, $precision, $trailingzeroes);\n$formatted = $x->formatnegative($number, $picture);\n$formatted = $x->formatpicture($number, $picture);\n$formatted = $x->formatprice($number, $precision, $symbol);\n$formatted = $x->formatbytes($number, $precision);\n$number    = $x->unformatnumber($formatted);\n\nuse Number::Format qw(:subs);\n$formatted = round($number, $precision);\n$formatted = formatnumber($number, $precision, $trailingzeroes);\n$formatted = formatnegative($number, $picture);\n$formatted = formatpicture($number, $picture);\n$formatted = formatprice($number, $precision, $symbol);\n$formatted = formatbytes($number, $precision);\n$number    = unformatnumber($formatted);\n",
            "subsections": []
        },
        "REQUIRES": {
            "content": "Perl, version 5.8 or higher.\n\nPOSIX.pm to determine locale settings.\n\nCarp.pm is used for some error reporting.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "These functions provide an easy means of formatting numbers in a manner suitable for displaying\nto the user.\n\nThere are two ways to use this package. One is to declare an object of type Number::Format,\nwhich you can think of as a formatting engine. The various functions defined here are provided\nas object methods. The constructor \"new()\" can be used to set the parameters of the formatting\nengine. Valid parameters are:\n\nTHOUSANDSSEP     - character inserted between groups of 3 digits\nDECIMALPOINT     - character separating integer and fractional parts\nMONTHOUSANDSSEP - like THOUSANDSSEP, but used for formatprice\nMONDECIMALPOINT - like DECIMALPOINT, but used for formatprice\nINTCURRSYMBOL   - character(s) denoting currency (see formatprice())\nDECIMALDIGITS    - number of digits to the right of dec point (def 2)\nDECIMALFILL      - boolean; whether to add zeroes to fill out decimal\nNEGFORMAT        - format to display negative numbers (def ``-x'')\nKILOSUFFIX       - suffix to add when formatbytes formats kilobytes (trad)\nMEGASUFFIX       -    \"    \"  \"    \"        \"         \"    megabytes (trad)\nGIGASUFFIX       -    \"    \"  \"    \"        \"         \"    gigabytes (trad)\nKIBISUFFIX       - suffix to add when formatbytes formats kibibytes (iec)\nMEBISUFFIX       -    \"    \"  \"    \"        \"         \"    mebibytes (iec)\nGIBISUFFIX       -    \"    \"  \"    \"        \"         \"    gibibytes (iec)\n\nThey may be specified in upper or lower case, with or without a leading hyphen ( - ).\n\nIf \"THOUSANDSSEP\" is set to the empty string, formatnumber will not insert any separators.\n\nThe defaults for \"THOUSANDSSEP\", \"DECIMALPOINT\", \"MONTHOUSANDSSEP\", \"MONDECIMALPOINT\", and\n\"INTCURRSYMBOL\" come from the POSIX locale information (see perllocale). If your POSIX locale\ndoes not provide \"MONTHOUSANDSSEP\" and/or \"MONDECIMALPOINT\" fields, then the \"THOUSANDSSEP\"\nand/or \"DECIMALPOINT\" values are used for those parameters. Formerly, POSIX was optional but\nthis caused problems in some cases, so it is now required. If this causes you hardship, please\ncontact the author of this package at <SwPrAwM@cpan.org> (remove \"SPAM\" to get correct email\naddress) for help.\n\nIf any of the above parameters are not specified when you invoke \"new()\", then the values are\ntaken from package global variables of the same name (e.g. $DECIMALPOINT is the default for the\n\"DECIMALPOINT\" parameter). If you use the \":vars\" keyword on your \"use Number::Format\" line\n(see non-object-oriented example below) you will import those variables into your namesapce and\ncan assign values as if they were your own local variables. The default values for all the\nparameters are:\n\nTHOUSANDSSEP     = ','\nDECIMALPOINT     = '.'\nMONTHOUSANDSSEP = ','\nMONDECIMALPOINT = '.'\nINTCURRSYMBOL   = 'USD'\nDECIMALDIGITS    = 2\nDECIMALFILL      = 0\nNEGFORMAT        = '-x'\nKILOSUFFIX       = 'K'\nMEGASUFFIX       = 'M'\nGIGASUFFIX       = 'G'\nKIBISUFFIX       = 'KiB'\nMEBISUFFIX       = 'MiB'\nGIBISUFFIX       = 'GiB'\n\nNote however that when you first call one of the functions in this module *without* using the\nobject-oriented interface, further setting of those global variables will have no effect on\nnon-OO calls. It is recommended that you use the object-oriented interface instead for fewer\nheadaches and a cleaner design.\n\nThe \"DECIMALFILL\" and \"DECIMALDIGITS\" values are not set by the Locale system, but are\ndefinable by the user. They affect the output of \"formatnumber()\". Setting \"DECIMALDIGITS\" is\nlike giving that value as the $precision argument to that function. Setting \"DECIMALFILL\" to a\ntrue value causes \"formatnumber()\" to append zeroes to the right of the decimal digits until\nthe length is the specified number of digits.\n\n\"NEGFORMAT\" is only used by \"formatnegative()\" and is a string containing the letter 'x',\nwhere that letter will be replaced by a positive representation of the number being passed to\nthat function. \"formatnumber()\" and \"formatprice()\" utilize this feature by calling\n\"formatnegative()\" if the number was less than 0.\n\n\"KILOSUFFIX\", \"MEGASUFFIX\", and \"GIGASUFFIX\" are used by \"formatbytes()\" when the value is\nover 1024, 1024*1024, or 1024*1024*1024, respectively. The default values are \"K\", \"M\", and \"G\".\nThese apply in the default \"traditional\" mode only. Note: TERA or higher are not implemented\nbecause of integer overflows on 32-bit systems.\n\n\"KIBISUFFIX\", \"MEBISUFFIX\", and \"GIBISUFFIX\" are used by \"formatbytes()\" when the value is\nover 1024, 1024*1024, or 1024*1024*1024, respectively. The default values are \"KiB\", \"MiB\", and\n\"GiB\". These apply in the \"iso60027\"\" mode only. Note: TEBI or higher are not implemented\nbecause of integer overflows on 32-bit systems.\n\nThe only restrictions on \"DECIMALPOINT\" and \"THOUSANDSSEP\" are that they must not be digits\nand must not be identical. There are no restrictions on \"INTCURRSYMBOL\".\n\nFor example, a German user might include this in their code:\n\nuse Number::Format;\nmy $de = new Number::Format(-thousandssep   => '.',\n-decimalpoint   => ',',\n-intcurrsymbol => 'DEM');\nmy $formatted = $de->formatnumber($number);\n\nOr, if you prefer not to use the object oriented interface, you can do this instead:\n\nuse Number::Format qw(:subs :vars);\n$THOUSANDSSEP   = '.';\n$DECIMALPOINT   = ',';\n$INTCURRSYMBOL = 'DEM';\nmy $formatted = formatnumber($number);\n",
            "subsections": []
        },
        "EXPORTS": {
            "content": "Nothing is exported by default. To export the functions or the global variables defined herein,\nspecify the function name(s) on the import list of the \"use Number::Format\" statement. To export\nall functions defined herein, use the special tag \":subs\". To export the variables, use the\nspecial tag \":vars\"; to export both subs and vars you can use the tag \":all\".\n",
            "subsections": []
        },
        "METHODS": {
            "content": "",
            "subsections": [
                {
                    "name": "new",
                    "content": "Creates a new Number::Format object. Valid keys for %args are any of the parameters\ndescribed above. Keys may be in all uppercase or all lowercase, and may optionally be\npreceded by a hyphen (-) character. Example:\n\nmy $de = new Number::Format(-thousandssep   => '.',\n-decimalpoint   => ',',\n-intcurrsymbol => 'DEM');\n"
                },
                {
                    "name": "round",
                    "content": "Rounds the number to the specified precision. If $precision is omitted, the value of the\n\"DECIMALDIGITS\" parameter is used (default value 2). Both input and output are numeric (the\nfunction uses math operators rather than string manipulation to do its job), The value of\n$precision may be any integer, positive or negative. Examples:\n\nround(3.14159)       yields    3.14\nround(3.14159, 4)    yields    3.1416\nround(42.00, 4)      yields    42\nround(1234, -2)      yields    1200\n\nSince this is a mathematical rather than string oriented function, there will be no trailing\nzeroes to the right of the decimal point, and the \"DECIMALPOINT\" and \"THOUSANDSSEP\"\nvariables are ignored. To format your number using the \"DECIMALPOINT\" and \"THOUSANDSSEP\"\nvariables, use \"formatnumber()\" instead.\n"
                },
                {
                    "name": "format_number",
                    "content": "Formats a number by adding \"THOUSANDSSEP\" between each set of 3 digits to the left of the\ndecimal point, substituting \"DECIMALPOINT\" for the decimal point, and rounding to the\nspecified precision using \"round()\". Note that $precision is a *maximum* precision\nspecifier; trailing zeroes will only appear in the output if $trailingzeroes is provided,\nor the parameter \"DECIMALFILL\" is set, with a value that is true (not zero, undef, or the\nempty string). If $precision is omitted, the value of the \"DECIMALDIGITS\" parameter\n(default value of 2) is used.\n\nIf the value is too large or great to work with as a regular number, but instead must be\nshown in scientific notation, returns that number in scientific notation without further\nformatting.\n\nExamples:\n\nformatnumber(12345.6789)             yields   '12,345.68'\nformatnumber(123456.789, 2)          yields   '123,456.79'\nformatnumber(1234567.89, 2)          yields   '1,234,567.89'\nformatnumber(1234567.8, 2)           yields   '1,234,567.8'\nformatnumber(1234567.8, 2, 1)        yields   '1,234,567.80'\nformatnumber(1.23456789, 6)          yields   '1.234568'\nformatnumber(\"0.000020000E+00\", 7);' yields   '2e-05'\n\nOf course the output would have your values of \"THOUSANDSSEP\" and \"DECIMALPOINT\" instead\nof ',' and '.' respectively.\n"
                },
                {
                    "name": "format_negative",
                    "content": "Formats a negative number. Picture should be a string that contains the letter \"x\" where the\nnumber should be inserted. For example, for standard negative numbers you might use\n``\"-x\"'', while for accounting purposes you might use ``\"(x)\"''. If the specified number\nbegins with a ``-'' character, that will be removed before formatting, but formatting will\noccur whether or not the number is negative.\n"
                },
                {
                    "name": "format_picture",
                    "content": "Returns a string based on $picture with the \"#\" characters replaced by digits from $number.\nIf the length of the integer part of $number is too large to fit, the \"#\" characters are\nreplaced with asterisks (\"*\") instead. Examples:\n\nformatpicture(100.023, 'USD ##,###.##')   yields   'USD    100.02'\nformatpicture(1000.23, 'USD ##,###.##')   yields   'USD  1,000.23'\nformatpicture(10002.3, 'USD ##,###.##')   yields   'USD 10,002.30'\nformatpicture(100023,  'USD ##,###.##')   yields   'USD ,*.'\nformatpicture(1.00023, 'USD #.###,###')   yields   'USD 1.002,300'\n\nThe comma (,) and period (.) you see in the picture examples should match the values of\n\"THOUSANDSSEP\" and \"DECIMALPOINT\", respectively, for proper operation. However, the\n\"THOUSANDSSEP\" characters in $picture need not occur every three digits; the *only* use of\nthat variable by this function is to remove leading commas (see the first example above).\nThere may not be more than one instance of \"DECIMALPOINT\" in $picture.\n\nThe value of \"NEGFORMAT\" is used to determine how negative numbers are displayed. The\nresult of this is that the output of this function my have unexpected spaces before and/or\nafter the number. This is necessary so that positive and negative numbers are formatted into\na space the same size. If you are only using positive numbers and want to avoid this\nproblem, set NEGFORMAT to \"x\".\n"
                },
                {
                    "name": "format_price",
                    "content": "Returns a string containing $number formatted similarly to \"formatnumber()\", except that\nthe decimal portion may have trailing zeroes added to make it be exactly $precision\ncharacters long, and the currency string will be prefixed.\n\nThe $symbol attribute may be one of \"INTCURRSYMBOL\" or \"CURRENCYSYMBOL\" (case\ninsensitive) to use the value of that attribute of the object, or a string containing the\nsymbol to be used. The default is \"INTCURRSYMBOL\" if this argument is undefined or not\ngiven; if set to the empty string, or if set to undef and the \"INTCURRSYMBOL\" attribute of\nthe object is the empty string, no currency will be added.\n\nIf $precision is not provided, the default of 2 will be used. Examples:\n\nformatprice(12.95)   yields   'USD 12.95'\nformatprice(12)      yields   'USD 12.00'\nformatprice(12, 3)   yields   '12.000'\n\nThe third example assumes that \"INTCURRSYMBOL\" is the empty string.\n"
                },
                {
                    "name": "format_bytes",
                    "content": ""
                },
                {
                    "name": "format_bytes",
                    "content": "Returns a string containing $number formatted similarly to \"formatnumber()\", except that\nlarge numbers may be abbreviated by adding a suffix to indicate 1024, 1,048,576, or\n1,073,741,824 bytes. Suffix may be the traditional K, M, or G (default); or the IEC standard\n60027 \"KiB,\" \"MiB,\" or \"GiB\" depending on the \"mode\" option.\n\nNegative values will result in an error.\n\nThe second parameter can be either a hash that sets options, or a number. Using a number\nhere is deprecated and will generate a warning; early versions of Number::Format only\nallowed a numeric value. A future release of Number::Format will change this warning to an\nerror. New code should use a hash instead to set options. If it is a number this sets the\nvalue of the \"precision\" option.\n\nValid options are:\n\nprecision\nSet the precision for displaying numbers. If not provided, a default of 2 will be used.\nExamples:\n\nformatbytes(12.95)                   yields   '12.95'\nformatbytes(12.95, precision => 0)   yields   '13'\nformatbytes(2048)                    yields   '2K'\nformatbytes(2048, mode => \"iec\")     yields   '2KiB'\nformatbytes(9999999)                 yields   '9.54M'\nformatbytes(9999999, precision => 1) yields   '9.5M'\n\nunit\nSets the default units used for the results. The default is to determine this\nautomatically in order to minimize the length of the string. In other words, numbers\ngreater than or equal to 1024 (or other number given by the 'base' option, q.v.) will be\ndivided by 1024 and $KILOSUFFIX or $KIBISUFFIX added; if greater than or equal to\n1048576 (1024*1024), it will be divided by 1048576 and $MEGASUFFIX or $MEBISUFFIX\nappended to the end; etc.\n\nHowever if a value is given for \"unit\" it will use that value instead. The first letter\n(case-insensitive) of the value given indicates the threshhold for conversion;\nacceptable values are G (for giga/gibi), M (for mega/mebi), K (for kilo/kibi), or A (for\nautomatic, the default). For example:\n\nformatbytes(1048576, unit => 'K') yields     '1,024K'\ninstead of '1M'\n\nNote that the valid values to this option do not vary even when the suffix configuration\nvariables have been changed.\n\nbase\nSets the number at which the $KILOSUFFIX is added. Default is 1024. Set to any value;\nthe only other useful value is probably 1000, as hard disk manufacturers use that number\nto make their disks sound bigger than they really are.\n\nIf the mode (see below) is set to \"iec\" or \"iec60027\" then setting the base option\nresults in an error.\n\nmode\nTraditionally, bytes have been given in SI (metric) units such as \"kilo\" and \"mega\" even\nthough they represent powers of 2 (1024, etc.) rather than powers of 10 (1000, etc.)\nThis \"binary prefix\" causes much confusion in consumer products where \"GB\" may mean\neither 1,048,576 or 1,000,000, for example. The International Electrotechnical\nCommission has created standard IEC 60027 to introduce prefixes Ki, Mi, Gi, etc.\n(\"kibibytes,\" \"mebibytes,\" \"gibibytes,\" etc.) to remove this confusion. Specify a mode\noption with either \"traditional\" or \"iec60027\" (or abbreviate as \"trad\" or \"iec\") to\nindicate which type of binary prefix you want formatbytes to use. For backward\ncompatibility, \"traditional\" is the default. See\nhttp://en.wikipedia.org/wiki/Binaryprefix for more information.\n"
                },
                {
                    "name": "unformat_number",
                    "content": "Converts a string as returned by \"formatnumber()\", \"formatprice()\", or \"formatpicture()\",\nand returns the corresponding value as a numeric scalar. Returns \"undef\" if the number does\nnot contain any digits. Examples:\n\nunformatnumber('USD 12.95')   yields   12.95\nunformatnumber('USD 12.00')   yields   12\nunformatnumber('foobar')      yields   undef\nunformatnumber('1234-567@.8') yields   1234567.8\n\nThe value of \"DECIMALPOINT\" is used to determine where to separate the integer and decimal\nportions of the input. All other non-digit characters, including but not limited to\n\"INTCURRSYMBOL\" and \"THOUSANDSSEP\", are removed.\n\nIf the number matches the pattern of \"NEGFORMAT\" *or* there is a ``-'' character before any\nof the digits, then a negative number is returned.\n\nIf the number ends with the \"KILOSUFFIX\", \"KIBISUFFIX\", \"MEGASUFFIX\", \"MEBISUFFIX\",\n\"GIGASUFFIX\", or \"GIBISUFFIX\" characters, then the number returned will be multiplied by\nthe appropriate multiple of 1024 (or if the base option is given, by the multiple of that\nvalue) as appropriate. Examples:\n\nunformatnumber(\"4K\", base => 1024)   yields  4096\nunformatnumber(\"4K\", base => 1000)   yields  4000\nunformatnumber(\"4KiB\", base => 1024) yields  4096\nunformatnumber(\"4G\")                 yields  4294967296\n"
                }
            ]
        },
        "CAVEATS": {
            "content": "Some systems, notably OpenBSD, may have incomplete locale support. Using this module together\nwith setlocale(3) in OpenBSD may therefore not produce the intended results.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "No known bugs at this time. Report bugs using the CPAN request tracker at\n<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Format> or by email to the author.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "William R. Ward, SwPrAwM@cpan.org (remove \"SPAM\" before sending email, leaving only my initials)\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "",
            "subsections": [
                {
                    "name": "perl",
                    "content": ""
                }
            ]
        }
    },
    "summary": "Number::Format - Perl extension for formatting numbers",
    "flags": [],
    "examples": [],
    "see_also": []
}