Implement AbsoluteValue function in stdlib
This commit is contained in:
parent
49902cda2a
commit
64a12a353c
1 changed files with 34 additions and 3 deletions
|
@ -13,15 +13,46 @@ namespace stdlib.functions
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
yield return new FunctionArgument() { };
|
yield return new FunctionArgument(
|
||||||
|
name: "value",
|
||||||
|
fv: new FunctionValue(ValueType.Numeric, 0m)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public FunctionValue ReturnValue => throw new NotImplementedException();
|
public FunctionValue ReturnValue => new FunctionValue(ValueType.Numeric, 0m);
|
||||||
|
|
||||||
public FunctionValue Execute(params FunctionArgument[] args)
|
public FunctionValue Execute(params FunctionArgument[] args)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
if (args == null || args.Length < 1 || args[0] == null || args[0].Value == null)
|
||||||
|
{
|
||||||
|
return new FunctionValue(ValueType.None, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var input = args[0].Value;
|
||||||
|
// Try to interpret both numeric and numeric-like string inputs
|
||||||
|
decimal number;
|
||||||
|
if (input.Type == ValueType.Numeric)
|
||||||
|
{
|
||||||
|
number = Convert.ToDecimal(input.Value);
|
||||||
|
}
|
||||||
|
else if (input.Type == ValueType.String && input.Value is string s)
|
||||||
|
{
|
||||||
|
number = Convert.ToDecimal(s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new FunctionValue(ValueType.None, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FunctionValue(ValueType.Numeric, Math.Abs(number));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new FunctionValue(ValueType.None, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue