Skip to content

Commit

Permalink
Add support for suffixes in expression input
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharparam committed Jan 2, 2025
1 parent c365bed commit 02968d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/changelog.txt
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 12 additions & 1 deletion src/scripts/expression.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 02968d2

Please sign in to comment.