summaryrefslogtreecommitdiff
path: root/Userland/Applications/Calculator
diff options
context:
space:
mode:
authorKarol Baraniecki <karol@baraniecki.eu>2022-12-26 17:38:27 +0100
committerAndreas Kling <kling@serenityos.org>2022-12-31 00:07:13 +0100
commit451ae985bf3284c0c9755031656e7795a2a37144 (patch)
treeb94d3283de59084a4ca18d594be523f053ed20cc /Userland/Applications/Calculator
parent21cc8f65f5c28a39843e1fa94ae819db74b1e5c8 (diff)
downloadserenity-451ae985bf3284c0c9755031656e7795a2a37144.zip
Calculator: Add adding/subtracting/multiplying/dividing by a percentage
It's now possible to easily calculate 50% of 50. :^)
Diffstat (limited to 'Userland/Applications/Calculator')
-rw-r--r--Userland/Applications/Calculator/Calculator.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/Userland/Applications/Calculator/Calculator.cpp b/Userland/Applications/Calculator/Calculator.cpp
index 9e68d1882a..929fabfd71 100644
--- a/Userland/Applications/Calculator/Calculator.cpp
+++ b/Userland/Applications/Calculator/Calculator.cpp
@@ -12,6 +12,12 @@
Optional<Crypto::BigFraction> Calculator::operation_with_literal_argument(Operation operation, Crypto::BigFraction argument)
{
+ // Support binary operations with percentages, for example "2+3%" == 2.06
+ if (m_binary_operation_in_progress != Operation::None && operation == Operation::Percent) {
+ argument = m_binary_operation_saved_left_side * Crypto::BigFraction { 1, 100 } * argument;
+ operation = Operation::None; // Don't apply the "%" operation twice
+ }
+
// If a previous operation is still in progress, finish it
// Makes hitting "1+2+3=" equivalent to hitting "1+2=+3="
if (m_binary_operation_in_progress != Operation::None) {
@@ -20,7 +26,8 @@ Optional<Crypto::BigFraction> Calculator::operation_with_literal_argument(Operat
switch (operation) {
case Operation::None:
- VERIFY_NOT_REACHED();
+ m_current_value = argument;
+ break;
case Operation::Add:
case Operation::Subtract: