Can we have decimal data type like in C# ?

Topics: Language Specification
Apr 5, 2014 at 5:10 PM
I would love to see decimal type in TypeScript . It would make Typescript better.
Apr 5, 2014 at 6:29 PM
TypeScript only allows you to specify JavaScript types (or TypeScript interfaces). JS just has "number", with no concept of integers or reals.

Apr 5, 2014 at 8:49 PM
Correct, decimal === number. They are both 64 bit floats.
Apr 6, 2014 at 8:19 AM
As far as I know decimal is 128 bit in C#.
Apr 6, 2014 at 1:24 PM
Edited Apr 6, 2014 at 1:25 PM
Yes, sorry, I was thinking "double" - must have been half asleep. Anyhow, if I recall, precision is probably why they cannot be the same. Someone may enter "decimal" for monetary calculations and find it is wrong. Go ahead and try typing "cents = 0.2 + 0.1" in any JS console and you will get "0.30000000000000004". Why? Because "double" fractions are binary related, and decimal are base 10. As well, "double" can hold more digits (because of the lower precision [and being binary based]).

Anyhow, most people would probably recommend using integers to store the whole number and fractional portions of monetary values, then combine them on output. There are many JavaScript libraries for this very purpose (BigNumber, BigDecimal, etc.).
Apr 7, 2014 at 12:12 PM
There's a suggestion out for this: https://typescript.codeplex.com/workitem/1858

I personally haven't voted for it, because I don't see the point for it, but that is from my usage perspective.