sq / JSIL

CIL to Javascript Compiler

Home Page:http://jsil.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C# decimal converted to double

fanoI opened this issue · comments

commented

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/

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++.

commented

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...

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