Format & parse a number with .numberformat()

.numberformat()


Description: Format and parse a number, formatting it the way you want.


Example:

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>numberformat demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
     
    <p class="formatted"></p>
     
    <script>
      bitsyCode(".formatted").html(bitsyCode.numberformat("5000"));
    </script>
     
    </body>
    </html>

Preview:

5,000.00
  • Usage

    • How to use:

      There are 2 ways of using this method, you may also specify custom options (View Example Usage below).

    • Element Value:

      Specify an element value which you want to format: (This takes the value of the element and formats it).

      bitsyCode.numberformat(bitsyCode(".myElement").val());
      Specified Value:

      Specify a value that you want to format: (This takes the value specified and formats it).

      bitsyCode.numberformat("5000");
    • Example Usage:
      bitsyCode.numberformat(bitsyCode(".myElement").val()); // Formats number from input element with class .myElement
      bitsyCode.numberformat("1500"); // Formats to 1,500.00, uses default options.
      bitsyCode.numberformat("1500", { decimals: 2, decimals_point:".", thousands_sep:" " }); // Formats to 1 500.00, uses options specified.
  • Options

    • What options are there:
      decimals: 2 (default value) /* Specify amount of decimals. */
      decimals_point: "." (default value) /* Specify the decimals separator. */
      thousands_sep: "," (default value) /* Specify the thousands separator. */
  • Returns

    • Returns:

      Formatted Value.

Share