11

I am working on a project in ASP.NET MVC. I started in the middle of the project and so I got stuck in this. The data type of a column in SQL Server is money. I need to access that column through the model so what will be its best equivalent datatype in C#?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kovid Purohit
  • 258
  • 1
  • 4
  • 15
  • 3
    `decimal` is the best choice for handling `money` datatype in C#. – Tetsuya Yamamoto Jul 21 '17 at 07:06
  • 5
    `money` is deprecated in SQL Server with a **strong** warning in the docs that it should never be used - since 2000. The appropriate SQL Server type is `numeric`. The equivalent .NET type is Decimal – Panagiotis Kanavos Jul 21 '17 at 07:17
  • @Tetsuya Yeah sure next time I'll make a deep search before asking. – Kovid Purohit Jul 21 '17 at 07:20
  • @KovidPurohit also consider that Bitcoin has 8 decimals. The Japanese Yen none. `money` has a hard-coded precision of 4 – Panagiotis Kanavos Jul 21 '17 at 07:38
  • @PanagiotisKanavos I can't find any reference in documentation that money is deprecated with a strong warning. Does anyone have a reference/source for this? – David Hoffman Dec 24 '21 at 12:43
  • @DavidHoffman this is know for almost a decade. The docs have moved around quite a bit in that time. [This RedGate article](https://www.red-gate.com/hub/product-learning/sql-prompt/avoid-use-money-smallmoney-datatypes) explains why `money` is avoided: it has very limited precision, it can lead to rounding errors, it's proprietary and despite the name *doesn't* store the currency. The article's example shows how easy it is to introduce errors in even a simple percentage. `199.5/271.0` is enough to cause a rounding error with `money` – Panagiotis Kanavos Dec 27 '21 at 18:58
  • @DavidHoffman besides, [one of the duplicate questions](https://stackoverflow.com/questions/582797/should-you-choose-the-money-or-decimalx-y-datatypes-in-sql-server) asks exactly what you asked and the top voted answer is `NEVER EVER use money`. The answers in support of `money` are simply unrealistic - where did people get the idea they don't need to multiply money in real life? How are the taxes in every bill calculated? Never mind that currencies don't just have 2 decimal digits (which become 4 decimals after multiplication). *Bitcoin* for example needs 8 decimal digits – Panagiotis Kanavos Dec 27 '21 at 19:08
  • @PanagiotisKanavos - I have seen those articles, and I'm not disagreeing with them (I'm using decimal). Based on your comment "money is deprecated in SQL Server with a strong warning in the docs that it should never be used" I was looking for the Microsoft Sql documentation that supports that statement, because if that exists, it completely shuts down any counter arguments. MONEY seems to be a hotly debated topic, some people say NEVER use it, others say you won't get precision issues unless you're going out to larger precision values, which most people won't hit. – David Hoffman Jan 03 '22 at 15:55

1 Answers1

13

System.Decimal. You can find other mappings in the documentation at https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping#NumericMapping

detaylor
  • 7,112
  • 1
  • 27
  • 46