gorules / zen

Open-source Business Rules Engine for your Rust, NodeJS, Python or Go applications.
https://gorules.io
MIT License
829 stars 79 forks source link

Trim decimal. #236

Open nimbit-software opened 2 months ago

nimbit-software commented 2 months ago

It would be great to have a function in the expressions for trimming decimal points. Something like this.


fn trim_decimal_places(num: f64, places: usize) -> f64 {
    let factor = 10_f64.powi(places as i32);
    (num * factor).trunc() / factor
}

Is it possible to add custom functions? Or how could i contribute additional functions?

ivanmiletic commented 2 months ago

Good suggestion, we may add it to backlog, for now you can use a workaround:

2 decimals: round(5.33333333 * 100) / 100
3 decimals: round(5.33333333 * 1000) / 1000