From 02968d24b6f1d180631ca61040a2287e372064f5 Mon Sep 17 00:00:00 2001 From: Adam Hellberg Date: Thu, 2 Jan 2025 11:00:31 +0100 Subject: [PATCH] Add support for suffixes in expression input --- src/changelog.txt | 6 ++++++ src/scripts/expression.lua | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/changelog.txt b/src/changelog.txt index 4185bcd..80ecfd1 100644 --- a/src/changelog.txt +++ b/src/changelog.txt @@ -1,4 +1,10 @@ --------------------------------------------------------------------------------------------------- +Version: 2.5.0 +Date: TBD + Minor Features: + - Add support for suffixes when using expression input (e.g. "1k" for 1 000 and "1M" for 1 000 000). + Following suffixes are supported: "k", "M", "G", "B" (all suffixes are case insensitive). +--------------------------------------------------------------------------------------------------- Version: 2.4.5 Date: 2024-12-16 Changes: diff --git a/src/scripts/expression.lua b/src/scripts/expression.lua index 54cc846..5788bde 100644 --- a/src/scripts/expression.lua +++ b/src/scripts/expression.lua @@ -3,6 +3,17 @@ local ssub = string.sub local expr = {} +local expr_vars = { + k = 1000, + K = 1000, + m = 1000000, + M = 1000000, + g = 1000000000, + G = 1000000000, + b = 1000000000, + B = 1000000000 +} + --- @param input string? --- @param fallback number? --- @return number @@ -15,7 +26,7 @@ function expr.parse(input, fallback) input = "0" .. input end - local success, result = pcall(helpers.evaluate_expression, input) + local success, result = pcall(helpers.evaluate_expression, input, expr_vars) if not success then log:warn("The given input '", input, "' could not be evaluated as a math expression: ", result)