'What is the equivalent datatype of SQL Server's Numeric in C#
In SQL Server we can write data AS Numeric(15,10) .. what will the equivalent of this in C#?
I know that Numeric's equivalent is Decimal but how to represent Numeric(15,10)?
Solution 1:[1]
Try looking at this site as a guide to the data type mappings. As far as the precision and length, you control that yourself using format specifiers
Solution 2:[2]
There are two answers depending on two questions:
1) What is something that allows you to specify the precision and scale. Nothing. This seems like your question, but just in case:
2) What is something that allows you to specify a decimal floating point number exactly. This is indeed the Decimal type -- but the point is internal and is set to one of 2^32 positions based on the input number. Not sure why, but only 28 values work, or 2^5 - 4..
So even though .Net allows the Decimal to look like a float, it is very different under the covers and does match the Decimal of SQLServer. Anything not a sum of distinct power of 2 values is an estimation using the normal binary floating point. This means even something such as the number 0.1, has already lost precision. But not with the Decimal type.
Solution 3:[3]
if you are using EntityFrameWorkCore there is a solution for this.
after defining DbContext in your project you can add configuration for the model as below:
public class ChequeEfConfiguration : IEntityTypeConfiguration<Cheque>
{
public void Configure(EntityTypeBuilder<Cheque> builder)
{
builder.Property(a => a.Amount).HasColumnType("decimal(18,2)");
}
}
or you can use OnModelCreating in your DbContext like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Cheque>().Property(x => x.Amount)
.IsRequired().HasColumnType("decimal(18,2)");
}
but I would recommend you to use the first one. for more information visit https://docs.microsoft.com/en-us/ef/core/modeling/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Icemanind |
| Solution 2 | Gerard ONeill |
| Solution 3 | mahdi yousefi |
