jQuery math finally solved

Web designers have long struggled with some of the more advanced aspects of computation and mathematics. Sometime ago, a Stack Overflow question asked

I’ve got a number in my JavaScript variable! Now how do I add another number to it? Please

A few well meaning answers point to using jQuery as the obvious solution. However, after a in depth search, I couldn’t find any actual jQuery plugins to do the calculations!

Well, there is a new jQuery plugin in town that aims to solve the problem of arithmetic in JavaScript: jMath!

It is incredibly easy to use in your code:

The plugin accepts the following operators:

  • ‘+’ (addition)
  • ‘-‘ (subtraction)
  • ‘*’ (multiplication)
  • ‘/’ (division)

And looking to add more in the future.

Hope that this helps in your development. Thanks!

Author: Michael Jasper

Michael is a Web Developer located in Northern Utah. When not programming, he enjoys spending time with his family, exploring the great-outdoors, and hit-and-miss cooking experiments.

  • Fred

    Why not just do it in JS it’s built in and your browser already supports it?

    • Not John Resig

      Because there might be cross-browser math issues and jQuery simplifies development. For example, with jMath, + would mean addition instead of concatenation.

      • bob brickburn

        The only math issue would be because the number is interpreted as a string. use the correct parse function when it applies. I would never add another function layer to do something as simple and, really, cross-browser compatible, that is simple math. Please!

  • Jonathan Sheppard

    This will be very helpful for browsers like Internet Explorer, which still use crayon-based math in Javascript. This JQuery solution will be a big help!

  • You should credit Bobince, the creator of that image.


  • Vajaninal Parser

    Oh come on! This is mind blowingly stupid. Is jQuery is some kind of cargo cult icon for low grade programmers?

    • Richard Vanbergen


      • Vajaninal Parser

        Duh! I am so sick of hearing about jQuery that it has poisoned my sense of humor. jQuery is a wonderful bit of tooling but it has indelibly been associated in my mind with moron ‘programmers’

  • The humor. I like.

  • Amaizing! Now Math in JS is much more simpler :). Thanks for that plugin!

  • Adam

    Oh man… This has to go up on github.

  • Kambfhase

    This plugin sucks. It seems to have some problems with elementary math. Doing
    $('#id').math('+', 0.1, 0.2) it gave me 0.30000000000000004 as the answer in Chrome. WHICH IS WRONG. I tried to fix that bug myself, but the code is horribly indented using tabs. I think, I will stick to the old API:

    • Gachl

      It’s not wrong, 0.1 and 0.2 are both float in JS and float is not a very precise data type. Most programming languages have the same behaviour, you gotta use some other decimal data type or round it.

      • Gachl, IEEE 754 floating point is in a lot of languages, *most* is a bit of a stretch, but I agree with Kambfhase – if you are creating a “math” library for js, it should handle returning floats in the same precision that it is sent to it. Lets remember, jQuery is widely adopted by folks that do not understand why .1 gets lost in translation between decimal and binary and back again.

        • I agree the point of a lib is to make these difficulties transparent to the user, if that is not accomplished you mine as well stick with good ‘ol JS.

          Side note: that stackoverflow image is the most comical thing I have ever seen. Tweeted and done.

          • Michael

            I would have hoped the fact that this was posted under Humor would have given a lot of people a clue that this wasn’t meant as a serious project.

      • Andrew Harrison


    • Aaron Mason

      Are you running that on a first release Pentium?

  • Pingback: The DOM isn’t slow, you are. | Wat, blogged()

  • epic


  • Ricky Yoder

    What did I just read?

  • Pingback: The DOM isn’t slow, you are. | Wat, blogged()