autoNumeric() - "just another jQuery™ plugin"

Please visit http://www.decorplanit.com/plugin/ for the latest version

Download: autoNumeric-1.4.5.js - 'heavy on comments'
Download: autoLoader.js

A flexible International numeric formatter, that will automatically place a thousand separator as you type and supports nine different rounding methods. Here are some of the key features on this jQuery™ plugin:

Implementing:

First - include the jQuery, autoNumeric (above) and autoLoader (above) javascript files in the header.

<script type="text/javascript" src="jQuery.js"></script> (version 1.3.2 or higher)
<script type="text/javascript" src="autoNumeric-1.4.3.js"></script>
<script type="text/javascript" src="autoLoader.js"></script>

Second - input fields can be aligned left or right. This can be done inline or via the auto class.

Third A - assign "auto" to the class of the input field that will use this plugin. This will produce the default settings - see below.

<input id="name" value="" type="text" class="auto" size="25" />

This demonstration shows the defaults when the alt attribute is left blank. The second and third columns show the strip and format functions respectively - more on this later.

$('input.auto').autoNumeric() $.fn.autoNumeric.Strip(ii) $.fn.autoNumeric.Format(ii, iv)

 
   

Third B - If you want to change the numeric format you will need to add the alt attribute as follows.

<input id="name" value="" type="text" class="auto" alt="p7s3c2S" size="25" />

The format code consists of 7 alpha numeric characters in the following sequence. Please note these are case sensitive. Also note that incomplete codes and incorrect codes can produce undesired results.

  1. First position can be either "p" (default) allows positive value only or '"n" allows positive or negative values.

  2. Second is numeric 0-9 (9 is the default) for the number of digits allowed to the left of the decimal separator. The value "0" allows for 15 integers (that is a big number!).

  3. Third character is used for the digital group separator:
    • a = apostrophe
    • c = comma (default)
    • p = period
    • s = space
    • x = none
  4. Fourth character is for the thousand digital grouping sequence
    • 3 digital grouping 123,123 (default).
    • 2 is for 12,12,123 where the first group is 3 followed by groupings of 2. This is used in India.
    • 4 will result is in digital groupings of four: 1234,1234 which is used in some far East Countries
  5. Fifth is for the decimal separator
    • c = comma
    • p = period (default).

  6. Sixth is numeric (0-9 or a thru Z) is for the number of decimal places:
    • 0 will also prevent the decimal character from being entered
    • 1-9 the number of decimal places (2 decimal places is the default)
    • a-Z lower or upper case. This is to look up the input field with the id = "dp[a-Z]" for the number of places in a input field. This allows the user to set their own decimal places. Multiple decimal fields are allowed but each must have their own unique id (examples dpa, dpb ....).
  7. Seventh is for the Rounding method used:
    • S = Round-Half-Up Symmetric (default)
    • A = Round-Half-Up Asymmetric
    • s = Round-Half-Down Symmetric
    • a = Round-Half-Down Asymmetric
    • B = Round-Half-Even "Bankers Rounding"
    • U = Round Up "Round-Away-From-Zero"
    • D = Round Down "Round-Toward-Zero"
    • C = Round to Ceiling "Toward Positive Infinity"
    • F = Round to Floor "Toward Negative Infinity"

The table below demonstrates the flexibility of the autoNumeric plugin and it's ability to format to different international standards.

The first column allows the user to to enter the value and formats as you type. Several formats are demonstrated.

The 2nd and 3rd columns are populated on the blur event, however you can choose any event or action.


$('input.auto').autoNumeric() $.fn.autoNumeric.Strip(ii) $.fn.autoNumeric.Format(ii, iv)
no alt attr
   

   

There are two public functions that are use in the above table that are called from another script.

The second column shows demonstrates the 'Strip' function. If the decimal separator is a comma it will be converted to a period. This takes one argument - the id of input field you want to have the formatting removed as the argument.

$.fn.autoNumeric.Strip('id of the input field that you want to remove the format');

The strip function can be used in the following ways:

The third column demonstrates the 'Format' function which requires two arguments. The target input id and the value (non formatted) to be formatted. Again this column is populated on the blur event, however you can choose any event or action.

$.fn.autoNumeric.Format('id of the target input field', 'value to be formatted');

The format function is implemented as follows:

Paste function try pasting various values including some with mixed text and numbers in the first column on the above table. Notes on the pasting abilities and limitations:

Rounding function - The below table is for testing the rounding methods that are supported. Experiment by entering various values in the upper left input field. The right column will display the 9 different rounding methods. Decimal places can range from 0 to 9.

For further details on rounding the DIY Calculator site is a excellent resource. Additional information can be found on Wikipedia.


Entered value to be rounded

  Rounded results
Round-Half-Up "Symmetric"
Decimal Places:
 
 
 
 
 
 
  Round to Floor "Toward Negative Infinity"

Version 1.4.5

Version 1.4.3

Versions 1.3.3 to 1.4.2: (requested custom versions)

Version 1.3.2:

Version 1.3.1:

Version 1.3.0:

Version 1.2.0 - 1.2.2: (please do not use and upgrade to 1.3.0 or greater)

Version 1.1.0 - abandoned

Original version 1.0.0 - 1.0.2 (please do not use and upgrade to 1.3.0 or greater)

Future improvements:

Limited testing conducted on the following browsers:

Please contact me is you have any questions, comments and or suggestions at bob@decorplanit.com