diff --git a/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs b/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs index 853b079..513dac9 100644 --- a/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs +++ b/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs @@ -34,30 +34,41 @@ public class HassiumConvert : HassiumObject { public HassiumConvert() { - Attributes.Add("toNumber", new InternalFunction(toNumber, 1)); + Attributes.Add("toDouble", new InternalFunction(toDouble, 1)); + Attributes.Add("toInt", new InternalFunction(toInt, 1)); Attributes.Add("toString", new InternalFunction(toString, 1)); Attributes.Add("toBool", new InternalFunction(toBool, 1)); } - public static HassiumObject toNumber(HassiumObject[] args) + public static HassiumObject toDouble(HassiumObject[] args) { if (args[0] is HassiumString) { - var ret = Convert.ToDouble(((HassiumString) args[0]).Value); - return ret == System.Math.Truncate(ret) ? new HassiumInt((int) ret) : new HassiumDouble(ret); + var ret = Convert.ToDouble(((HassiumString)args[0]).Value); + return ret == System.Math.Truncate(ret) ? new HassiumInt((int)ret) : new HassiumDouble(ret); } else if (args[0] is HassiumInt) - { - return new HassiumDouble(((HassiumInt) args[0]).Value); - } + return new HassiumDouble(((HassiumInt)args[0]).Value); else if (args[0] is HassiumDouble) - { - return new HassiumInt(((HassiumDouble) args[0]).ValueInt); - } + return args[0]; + else if (args[0] is HassiumByte) + return new HassiumDouble(Convert.ToDouble(((HassiumByte)args[0]).Value)); else - { - throw new Exception("Unknown format for Convert.toNumber"); - } + throw new Exception("Unknown format for Convert.toDouble()"); + } + + public static HassiumObject toInt(HassiumObject[] args) + { + if (args[0] is HassiumString) + return Convert.ToInt32(((HassiumString)args[0]).Value); + else if (args[0] is HassiumDouble) + return new HassiumInt(((HassiumDouble)args[0]).ValueInt); + else if (args[0] is HassiumInt) + return args[0]; + else if (args[0] is HassiumByte) + return new HassiumInt(Convert.ToInt32(((HassiumByte)args[0]).Value)); + else + throw new Exception("Unknown format for Convert.toInt()"); } public static HassiumObject toString(HassiumObject[] args)