diff --git a/src/helper.rs b/src/helper.rs index 26304ca..d13138c 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -7,6 +7,22 @@ use evalexpr::{ use crate::util; // Mathematics +pub fn fix(arg: &Value) -> Result { + let args = arg.as_tuple()?; + + let count = args.len(); + if count != 2 { + return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2, actual: count }); + } + + let float = args[0].as_float()?; + let figures = args[1].as_int()?; + + let operand: f64 = i64::pow(10, figures as u32) as f64; + let output = f64::round(float * operand) / operand; + return Ok(output.into()); +} + pub fn logarithm(arg: &Value) -> Result { let arguments: Vec; let count: usize; diff --git a/src/main.rs b/src/main.rs index b365b71..8a0c842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ fn main() { "√2" => global::ROOT_TWO, // math functions + "fix" => Function::new(|arg| helper::fix(arg)), "log" => Function::new(|arg| helper::logarithm(arg)), "sqrt" => Function::new(|arg| helper::square_root(arg)),