0

In SQL Server I have a primary key of a Table declared as follow :

[CustomerID] [numeric](18, 0) IDENTITY(1000000,1) NOT NULL,

In ASP.NET Core 2.2 model i mapped this field ad follow :

public decimal CustomerId { get; set; }

How can I make the decimal digits not appear in the application?

3
  • When you have a decimal value there is always the decimal digits in it, you have to format it before displaying. basically the representation of that value can be changed according to the formatting. Commented Oct 4, 2019 at 9:03
  • @Siavash : It isn't possible to do something with annotations? Commented Oct 4, 2019 at 9:07
  • 1
    of course, it is, that formatting can take place anywhere. I see there is already an answer using annotations. Commented Oct 4, 2019 at 14:33

2 Answers 2

1

Use DisplayFormat attribute:

[DisplayFormat(DataFormatString = "{0:0}", ApplyFormatInEditMode =true)]
public decimal CustomerId { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

0

If you only need to display it without decimal places, then the formatting can be applied with a ToString anywhere, preferably where you are trying to display it. The code below will apply rounding.

CustomerId.ToString("0");

If you need to use it in some other capacity without the decimal points then you should map your model representing the database table to another model specific to what you are trying to do and supply the correct type in that model. Probably an int or string in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.