jQuery Calculation Plug-in (v0.1)

The Calculation plug-in is designed to give easy-to-use jQuery functions for commonly used mathematical functions.

This plug-in will work on all types of HTML elements—which means you can use it to calculate values in <td> elements or in <input> elements. You can even mix and match between element types.

Numbers are parsed from the element using parseNumber() method—which uses a regular expression (/(-?\$?)(\d+(,\d{3})*(\.\d{1,})?|\.\d{1,})/g) to parse out the numeric value. You can change the regular expression that's used to determine what's consider a number by changing the default regular expression.

If you're page is using European-style formatting (i.e. 1.000,00) you'll need to change the default parsing handlers for European formatting. The calculation plug-in should be powerful enough to address any style formatting your numbers might be in. For US-style formatting, no changes are needed (it assumes numbers are in the format 1,000.00).

Download

Download the plug-in:
jquery.calculation.js
jquery.calculation.min.js

Interactive Examples

Calculation Examples

The Calculation plug-in can parse various DOM elements. From normal div and span tags to all form field elements.

Numbers:    Sum: (Change the values for dynamic calculations.)

Numbers:    Average: (Change the values for dynamic calculations.)

NOTE: The above example uses the onParseError and onParseClear methods to change the background of any field in which it can not correctly parse the number from. To test this, try changing a field to all numeric values.

Numbers:    Min: (Change the values for dynamic calculations.)

Numbers:    Max: (Change the values for dynamic calculations.)

Numbers: -$19.99 + -$7.99 + -$.99 + -$49.99    Sum: ????

Numbers: 3,294.75 + 85.97 + 2974.25 + $6,502.03    Avg: ????


The example below shows how the calculation plug-in can be used to dynamically calculate values for you. All of the "Total" values (including the "Grand Total") are calculated automatically using the calc() method.

Qty Product Price Total
Learning jQuery $39.99 $39.99
jQuery Donation $14.99 $14.99
Grand Total:

Syntax Examples