Simple Calculator Input

You can enter expressions into the boxes (e.g. "100e-9*100e-9") and use SI number postfixes (e.g. "1k" or "4.7n").

Examples:

Si numbers: =
Basic Maths: =
More Maths: =

Simple applications are engineering style calculators. E.g. This ohms law calculator: from six 3.3 volt cells over a 47k resistor can be written with ease:

V: Volts
R: Ohms
I = A

API:

This is available as var myNumber = MJSIO.getNumber(elementID) from the element ID of an input box. MJSIO also provides MJSIO.setNumber(elementID,myNumber[,precision]) to set the textContent of an element to a number written in engineering form. Precision defaults to 5. I've used this above to set the contence of a span to just what is read from the input.

A class MJSIOFAIL is added to the input if the input fails to evaluate. I've put .MJSIOFAIL{ color:red; } into the style of this page.

Why

To make things better. I've made some relativly bug free code so you don't have too. It does this one thing, well enough.

Notes:

The si postfixes on numbers are worked out by simply replacing any valid SI postfix that occurs directly after a number with it's corresponding exponent. This means operator precedence is correct. Then the expression is ran eval-style with basic filtering.

Many maths functions are moved to the local namespace so you can use: abs, acos, asin, atan, ceil, cos, exp, floor, pow, random, round, sin, sqrt, and tan. ln is natural log, and log is log10. Constants e and pi are available too.

Hover over the text input to see how the numbers are interpreted. This is done by setting the .title to the result, so desktop users can see the result of the input without the developer having to code some sort of mirror.

These cases should fail:

bad input:
bad input:
bad input:

Use it:

Include <script src="mjsio.js"></script> or copy and paste this:

If you like this, please stop by the github page.

I release this code under the WTFPL version 2 licence.