mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 08:02:28 +00:00
feat: trunc function and decimal places for round (optional) (#349)
This commit is contained in:
@@ -35,6 +35,7 @@ pub enum InternalFunction {
|
||||
Floor,
|
||||
Ceil,
|
||||
Round,
|
||||
Trunc,
|
||||
|
||||
// Type
|
||||
IsNumeric,
|
||||
@@ -173,9 +174,32 @@ impl From<&InternalFunction> for Rc<dyn FunctionDefinition> {
|
||||
signature: FunctionSignature::single(VT::Number, VT::Number),
|
||||
}),
|
||||
|
||||
IF::Round => Rc::new(StaticFunction {
|
||||
IF::Round => Rc::new(CompositeFunction {
|
||||
implementation: Rc::new(imp::round),
|
||||
signature: FunctionSignature::single(VT::Number, VT::Number),
|
||||
signatures: vec![
|
||||
FunctionSignature {
|
||||
parameters: vec![VT::Number],
|
||||
return_type: VT::Number,
|
||||
},
|
||||
FunctionSignature {
|
||||
parameters: vec![VT::Number, VT::Number],
|
||||
return_type: VT::Number,
|
||||
},
|
||||
],
|
||||
}),
|
||||
|
||||
IF::Trunc => Rc::new(CompositeFunction {
|
||||
implementation: Rc::new(imp::trunc),
|
||||
signatures: vec![
|
||||
FunctionSignature {
|
||||
parameters: vec![VT::Number],
|
||||
return_type: VT::Number,
|
||||
},
|
||||
FunctionSignature {
|
||||
parameters: vec![VT::Number, VT::Number],
|
||||
return_type: VT::Number,
|
||||
},
|
||||
],
|
||||
}),
|
||||
|
||||
IF::Sum => Rc::new(StaticFunction {
|
||||
@@ -404,7 +428,24 @@ pub(crate) mod imp {
|
||||
|
||||
pub fn round(args: Arguments) -> anyhow::Result<V> {
|
||||
let a = args.number(0)?;
|
||||
Ok(V::Number(a.round()))
|
||||
let dp = args
|
||||
.onumber(1)?
|
||||
.map(|v| v.to_u32().context("Invalid number of decimal places"))
|
||||
.transpose()?
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok(V::Number(a.round_dp(dp)))
|
||||
}
|
||||
|
||||
pub fn trunc(args: Arguments) -> anyhow::Result<V> {
|
||||
let a = args.number(0)?;
|
||||
let dp = args
|
||||
.onumber(1)?
|
||||
.map(|v| v.to_u32().context("Invalid number of decimal places"))
|
||||
.transpose()?
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok(V::Number(a.trunc_with_scale(dp)))
|
||||
}
|
||||
|
||||
pub fn rand(args: Arguments) -> anyhow::Result<V> {
|
||||
|
||||
@@ -353,6 +353,7 @@ impl From<&Node<'_>> for UnaryNodeBehaviour {
|
||||
InternalFunction::Floor => CompareWithReference(Equal),
|
||||
InternalFunction::Ceil => CompareWithReference(Equal),
|
||||
InternalFunction::Round => CompareWithReference(Equal),
|
||||
InternalFunction::Trunc => CompareWithReference(Equal),
|
||||
InternalFunction::String => CompareWithReference(Equal),
|
||||
InternalFunction::Number => CompareWithReference(Equal),
|
||||
InternalFunction::Bool => CompareWithReference(Equal),
|
||||
|
||||
@@ -432,4 +432,37 @@ x not in [1..10];{"x":5};false
|
||||
|
||||
# Interval iterator
|
||||
map([0..5], # * 2);;[0, 2, 4, 6, 8, 10]
|
||||
filter([0..10], # % 2 == 0);;[0, 2, 4, 6, 8, 10]
|
||||
filter([0..10], # % 2 == 0);;[0, 2, 4, 6, 8, 10]
|
||||
|
||||
# Rounding
|
||||
round(7.4);; 7
|
||||
round(7.5);; 8
|
||||
round(7.6);; 8
|
||||
round(-7.4);; -7
|
||||
round(-7.5);; -8
|
||||
round(-7.6);; -8
|
||||
|
||||
round(7.444, 0);; 7
|
||||
round(7.444, 1);; 7.4
|
||||
round(7.444, 2);; 7.44
|
||||
round(7.555, 0);; 8
|
||||
round(7.555, 1);; 7.6
|
||||
round(7.555, 2);; 7.56
|
||||
round(-7.444, 2);; -7.44
|
||||
round(-7.555, 2);; -7.56
|
||||
|
||||
trunc(7.4);; 7
|
||||
trunc(7.5);; 7
|
||||
trunc(7.6);; 7
|
||||
trunc(-7.4);; -7
|
||||
trunc(-7.5);; -7
|
||||
trunc(-7.6);; -7
|
||||
|
||||
trunc(7.444, 0);; 7
|
||||
trunc(7.444, 1);; 7.4
|
||||
trunc(7.444, 2);; 7.44
|
||||
trunc(7.999, 0);; 7
|
||||
trunc(7.999, 1);; 7.9
|
||||
trunc(7.999, 2);; 7.99
|
||||
trunc(-7.444, 2);; -7.44
|
||||
trunc(-7.999, 2);; -7.99
|
||||
|
Can't render this file because it contains an unexpected character in line 14 and column 13.
|
Reference in New Issue
Block a user