JavaScript Calculator

?

Floating-point precision workaround

Because JavaScript is a dynamically-typed language, there is no specific floating-point number type (i.e. Decimal in C#). Fractions such as 1/5 or 1/10 will not round "cleanly", like they will in a floating-point data type.

For example, in JavaScript, 0.1 + 0.2 = 0.30000000000000004, instead of the expected 0.3. To work around this, rather than implementing a library such as Decimal.js, I implemented a very basic workaround - which is to multiply each number value in an expression by 10, evaluate the expression, then divide the total by 10.

This works in most scenarios (at least in this simple application), but in a real-world situation, I would use a JavaScript library that has been tested thoroughly.

You can find more info on this issue here: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

TL;DR

If you start getting unexpected answers with this calculator, i.e. tons of extra decimal places, try the workaround!

Calculator Info

I created this calculator while using the Windows 10 Calculator app as a model. Almost everything I've done in terms of layout and functionality is based on that. I don't plan on adding more complex functionality, such as memory, scientific functions, etc., since I just created this project to get some practice in with JavaScript.

Keyboard Shortcuts
0-9: Numbers 0-9
Numpad 0-9: Numbers 0-9
Numpad /: Divide
Numpad *: Multiply
Numpad -: Subtract
Numpad +: Add
Numpad Enter: Equals
Enter/Return: Equals
Esc: Clear All (C Button)
Del: Clear Last (CE Button)
Backspace: Backspace (BACK Button)