sq / JSIL

CIL to Javascript Compiler
http://jsil.org/
Other
1.73k stars 241 forks source link

C# decimal converted to double #1004

Open fanoI opened 8 years ago

fanoI commented 8 years ago

I've tried this simple C# code and sadly I got the wrong result:

`` using System; using JSIL; using JSIL.Meta;

public static class Program {

public static void Main () { decimal d1 = 0.2m; decimal result = d1 * 3;

Console.WriteLine("result is " + result + " not 0.6!");

} } `` The output is: result is 0.6000000000000001 not 0.6!

The '0.2m' in javascript is converted to double implicity using 'var', it will be better to convert it to a proper javascript Decimal using this library: https://mikemcl.github.io/decimal.js/

iskiselev commented 8 years ago

Unfortunately, even that library differs from .Net decimal (it's arbitrary precious and doesn't keep trailing zeros for example). Correct way will be to reimplement decimal type from .Net in JavaScript, but it could not be done automatically, as it has non-managed implementation in C++.

fanoI commented 8 years ago

In CoreRt Microsoft have re-implemented Decimal as a pure C# type using this class for the low level stuff: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/src/System/Decimal.DecCalc.cs

I'm not sure if the implementation is complete being CoreRt yet in alpha state...

iskiselev commented 8 years ago

@fanol, many thanks for this information. I'll try to integrate it with JSIL when will have some free time.