Percentage
The WebAssembly module exports a percentage function that takes a value and a percent and returns the value multiplied by the percent divided by 100. It is a plain function rather than a method, so it does not touch the calculator's stored value, memory register, or history. It has no button on the keypad; it is available to any JavaScript that imports the module directly.
What it returns
The function is deliberately the simplest possible definition of a percentage, with no rounding and no special cases. Both arguments are 64-bit floats and so is the result.
- 10 percent of 200 returns 20.
- 50 percent of 50 returns 25.
- 0 percent of any value returns 0, and any percent of 0 returns 0.
- 200 percent of 50 returns 100; values above 100 are not clamped.
- A negative value or a negative percent returns a negative result; 10 percent of −100 returns −10.
Each of those cases is asserted by a unit test in the crate, so the behaviour above is checked rather than described.
Computing a percentage on the keypad
Because there is no percent key, percentages are done with the keys that exist. To find 15 percent of 240, enter 240, press ×, enter 15, press =, then press ÷, enter 100 and press = again. The result is 36. To add a percentage rather than take one, compute the percentage first, store it with MS, recall the original value, and add the stored figure back with the arithmetic keys.
Both routes go through the same four arithmetic operators as any other calculation, so each step lands in the history panel and each one is subject to the same 64-bit floating point rules described on the operations page. A percentage of a percentage compounds the same rounding, so a long chain of percentage steps can drift in the last digit or two.
Questions and answers
Does the Rust WASM Calculator have a percent button?
No. The percentage function is exported by the WebAssembly module and can be imported from JavaScript, but the on-screen keypad has no percent key. You can compute a percentage with multiplication and division instead.
Does the percentage function round its result?
No. It multiplies the value by the percent divided by 100 in 64-bit floating point and returns the result unrounded.