Implement AbsoluteValue function in stdlib

This commit is contained in:
Jordan Wages 2025-08-19 23:50:19 -05:00
commit 64a12a353c

View file

@ -13,15 +13,46 @@ namespace stdlib.functions
{
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)
{
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);
}
}
}
}