2

I have to set 3 decimal place in SQL Server 2005.i can set it from only model part .But In sql server set datatype of Salary property is decimal.

 When I set like

         public decimal Salary { get; set; }

It would be saved to the database like 21000.000 not 21000. If I put 30000.75, then it would be saved to 30000.750.

sarvesh kushwaha
  • 342
  • 1
  • 3
  • 19
Monica
  • 607
  • 4
  • 12
  • 31

4 Answers4

1

I also got stuck in the same situation and finally found the solution. You can override the DbContext.OnModelCreating() method and configure the precision

public class ApplicationDbContext : IdentityDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity().Property(c => c.AssociationFee).HasPrecision(18, 3);
    }
}

Hope this helps

Muhammad Waqas Iqbal
  • 3,244
  • 1
  • 20
  • 9
0

Apply this attribute to the property in your model:

[DisplayFormat(DataFormatString = "{0:#,##0}")]
Moho
  • 15,457
  • 1
  • 30
  • 31
0

You could use the MVC DisplayFormat Attribute:

DisplayFormat not getting applied on decimal value

Or for display purposes you could use a string and do:

Salary.ToString("F");

Which would result in the output: 30000.75

Or:

Salary.ToString("N");

Which would result in the output: 30,000.75

Community
  • 1
  • 1
Darren
  • 68,902
  • 24
  • 138
  • 144
0

Try this one , you have to set in edit mode also .

 [DisplayFormat(DataFormatString = "{0:n3}", ApplyFormatInEditMode = true)]
 public decimal Amount { get; set; }
sarvesh kushwaha
  • 342
  • 1
  • 3
  • 19