Exemplo n.º 1
0
        /// <summary>
        /// Converts an object to float
        /// </summary>
        /// <param name="obj">The object to be converted</param>
        /// <returns>The object converted</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="obj"/> is null</exception>
        /// <exception cref="InterpreterException">If <paramref name="obj"/> can't be converted to float</exception>
        public static KFloat ToFloat(KObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            KNumber number = obj as KNumber;

            if (number != null)
            {
                return(new KFloat(number.ToFloat()));
            }
            KString str = obj as KString;

            if (str != null)
            {
                float res;
                if (float.TryParse(str, out res))
                {
                    return(new KFloat(res));
                }
            }
            ThrowHelper.TypeError($"Can't convert {obj.GetType().Name} to float");
            return(null);
        }
Exemplo n.º 2
0
        public int CompareTo(object obj)
        {
            KNumber num = obj as KNumber;

            if (num != null)
            {
                return(((IComparable)Value).CompareTo(num.Value));
            }
            ThrowHelper.TypeError("Object is not a number");
            return(0);
        }