Script engines usually support just one numeric datatype, but .NET has several, and that creates an ambiguity when it comes to importing numeric values from script. ClearScript's policy is to preserve
values unless they can be converted to
without data loss.
You may be wondering why ClearScript does any data conversion at all. Actually, it aims to do as little data conversion as possible, but this is a special case. This particular conversion is very often desirable because
is an extremely common .NET datatype and is implicitly convertible back to
if that's what's required.
ClearScript's behavior is not ideal in your situation, but in many cases script code uses
because it's the only available numeric datatype, not because it's actually required. Without this conversion many common script-to-host scenarios wouldn't work; e.g., passing an integer literal to a method with an
If your application expects a
result from script evaluation, we recommend the following:
var result = Convert.ToDouble(engine.Evaluate(code));