Пример #1
0
        // https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-value
        // Convert string to number
        public static FormulaValue Value(EvalVisitor runner, SymbolContext symbolContext, IRContext irContext, FormulaValue[] args)
        {
            var arg0 = args[0];

            if (arg0 is NumberValue n)
            {
                return(n);
            }

            if (arg0 is DateValue dv)
            {
                return(DateToNumber(irContext, new DateValue[] { dv }));
            }

            if (arg0 is DateTimeValue dtv)
            {
                return(DateTimeToNumber(irContext, new DateTimeValue[] { dtv }));
            }

            var str    = ((StringValue)arg0).Value.Trim();
            var styles = NumberStyles.Any;

            if (string.IsNullOrEmpty(str))
            {
                return(new BlankValue(irContext));
            }

            double div = 1;

            if (str[str.Length - 1] == '%')
            {
                str    = str.Substring(0, str.Length - 1);
                div    = 100;
                styles = NumberStyles.Number;
            }
            else if (str[0] == '%')
            {
                str    = str.Substring(1, str.Length - 1);
                div    = 100;
                styles = NumberStyles.Number;
            }

            if (!double.TryParse(str, styles, runner.CultureInfo, out var val))
            {
                return(CommonErrors.InvalidNumberFormatError(irContext));
            }

            if (IsInvalidDouble(val))
            {
                return(CommonErrors.ArgumentOutOfRange(irContext));
            }

            val /= div;

            return(new NumberValue(irContext, val));
        }