4

I have a question for decimal value for set on model. In my SQL server, displaySize field type is decimal(18,1) How can I set the decimal type in model?

When I set like

 public decimal displaySize { get; set; }

It display like 21.00 not 21. If I put 3.7, then it come out 3.70.

How do I set decimal format? It sounds simple question, but I am beginner MVC 3 currently. Could you give some answer? Thanks.

2 Answers 2

11

You can use the DisplayFormatAttribute on your model property:

[DisplayFormat(DataFormatString = "{0:0.###}")]
public decimal displaySize { get; set; }

The above will output a number with up to 3 decimal places.

Other custom numeric formats and standard numeric formats can also be used.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Your solution solves my headache.
5

You could define a display format:

[DisplayFormat(DataFormatString = "{0:0.##}", ApplyFormatInEditMode = true)]
public decimal displaySize { get; set; }

and then in your strongly typed view:

@Html.DisplayFor(x => x.displaySize)

or:

@Html.EditorFor(x => x.displaySize)

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.