...
This commit is contained in:
32
rhai_engine/examples/loadscripts/scripts/math_utils.rhai
Normal file
32
rhai_engine/examples/loadscripts/scripts/math_utils.rhai
Normal file
@@ -0,0 +1,32 @@
|
||||
// Math utility functions for Tera templates
|
||||
|
||||
// Format a number with commas as thousands separators
|
||||
fn format_number(num) {
|
||||
let str = num.to_string();
|
||||
let result = "";
|
||||
let len = str.len;
|
||||
|
||||
for i in 0..len {
|
||||
if i > 0 && (len - i) % 3 == 0 {
|
||||
result += ",";
|
||||
}
|
||||
result += str.substr(i, 1);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
// Calculate percentage
|
||||
fn percentage(value, total) {
|
||||
if total == 0 {
|
||||
return "0%";
|
||||
}
|
||||
|
||||
let pct = (value / total) * 100.0;
|
||||
pct.round().to_string() + "%"
|
||||
}
|
||||
|
||||
// Format currency
|
||||
fn currency(amount, symbol) {
|
||||
symbol + format_number(amount.round(2))
|
||||
}
|
Reference in New Issue
Block a user