diff --git a/Upsilon/BaseTypes/UserData/UpsilonBinder.cs b/Upsilon/BaseTypes/UserData/UpsilonBinder.cs index 54c145e..3ab3962 100644 --- a/Upsilon/BaseTypes/UserData/UpsilonBinder.cs +++ b/Upsilon/BaseTypes/UserData/UpsilonBinder.cs @@ -130,19 +130,31 @@ namespace Upsilon.BaseTypes.UserData return s.Value; } break; - case TypeCode.Byte: + case TypeCode.Single: case TypeCode.Decimal: case TypeCode.Double: + if (value is ScriptNumber number) + { + return Convert.ChangeType(number.ToCSharpObject(), typeCode); + } + break; + case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: - case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: if (value is ScriptNumber numeric) { + if (numeric.IsFloat) + { + // We expect a conversion from a float to an integer to just lose its precision, however + // Convert.ChangeType will round it to the nearest integer. Therefore we floor the value + // before converting it + return Convert.ChangeType(Math.Floor((double) numeric.ToCSharpObject()), typeCode); + } return Convert.ChangeType(numeric.ToCSharpObject(), typeCode); } break;